JavaScript Filter unique objects in array based on timestamp condition filter unique objects in array javascript,filter unique objects in

I have the following array:

let arr = [
    {"id": 123, "lastUpdate": 1543229793},
    {"id": 456, "lastUpdate": 1545269320},
    {"id": 123, "lastUpdate": 1552184795}
]

I need to filter the array based on the same ID, but also check the "lastUpdate" timestamp and keep only the newer entries. The result should be:

[
    {"id": 456, "lastUpdate": 1545269320},
    {"id": 123, "lastUpdate": 1552184795}
]

I have tried the following:

arr = arr.filter((e, index, self) =>
    index === self.findIndex((t) => (
        t.id === intent.id && t.lastUpdate > e.lastUpdate
    ))
)

However, this filters everything for me and the resulting array is empty. I think something is wrong with the last part of above && t.lastUpdate > e.lastUpdate.

Many thanks for any tips!

Answer:1

Hi there if you are looking for a performant solution you can use an object :)

let arr = [{"id": 123,"lastUpdate": 1543229793},
{"id": 456,"lastUpdate": 1545269320},
{"id": 123, "lastUpdate": 1552184795}];

let newArr = {}
arr.forEach(el => {
  if(!newArr[el.id] || newArr[el.id].lastUpdate < el.lastUpdate){
      newArr[el.id] = el
  }
})

console.log(Object.values(newArr));
Answer:2

You can achieve it by looking for items that don't have an item2 where the update was later

   arr.filter(item => 
                 { return !arr.some(item2 => 
                  item.id === item2.id && item.lastUpdate < item2.lastUpdate)
            });

What that code does is :

For each item in the array it look if in the array there is an item with the same id where the lastUpdate is superior to its own. If there is one it return true (Array.some returns a boolean). We negate that value and use it to filter.

Answer:3

You could do it step by step by converting to a set, sorting then getting the first item for each id:

let arr = [
    {"id": 123, "lastUpdate": 1543229793},
    {"id": 456, "lastUpdate": 1545269320},
    {"id": 123, "lastUpdate": 1552184795}
]

// Get the ids by making a set of ids and then converting to array
let ids = [ ...new Set(arr.map((e) => e.id)) ];

// Sort the original by lastUpdate descending
arr.sort((a, b) => b.lastUpdate - a.lastUpdate);

// Get array of first item from arr by id
let res = ids.map(id => arr.find((e) => e.id == id));

console.log(res);
Answer:4

Is there anyway to stream an audio file without giving complete path of the file. For example whenever we tried to play mp3 file with HTML5 Audio tag or With javascript, then i have to supply ...

Is there anyway to stream an audio file without giving complete path of the file. For example whenever we tried to play mp3 file with HTML5 Audio tag or With javascript, then i have to supply ...

  1. stream audio files
  2. stream audio files php
  3. stream audio files on ipad
  4. microsoft stream audio files
  5. stream wav audio files
  6. stream audio/video files
  7. vlc stream audio files
  8. gta sa audio stream files
  9. stream audio files dropbox

I have two .env files like dev.env and staging.env. I am using typeorm as my database ORM. I would like to know how to let typeorm read either of the config file whenever I run the application. Error: ...

I have two .env files like dev.env and staging.env. I am using typeorm as my database ORM. I would like to know how to let typeorm read either of the config file whenever I run the application. Error: ...

  1. nestjs typeorm configuration
  2. nestjs typeorm config

I have a reference to a component <Gmap ref="mapRef"> In mounted I am doing this, just to see the objects are available mounted(){ let self = this console.log(self.$refs) // Shows the ...

I have a reference to a component <Gmap ref="mapRef"> In mounted I am doing this, just to see the objects are available mounted(){ let self = this console.log(self.$refs) // Shows the ...

I developed in and published a game to the Play Store made with the Phaser gaming engine and Cordova. When this game is good enough, I would like to publish it to the App Store, but I've read that ...

I developed in and published a game to the Play Store made with the Phaser gaming engine and Cordova. When this game is good enough, I would like to publish it to the App Store, but I've read that ...

  1. will apple approve me for financing
  2. does apple approve apps over the weekend
  3. does apple approve webview apps
  4. does apple approve apps on weekends
  5. does apple approve of malwarebytes
  6. will apple card approved me