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!
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!
Share Improve this question edited Jan 25, 2019 at 14:03 user10962926 asked Jan 25, 2019 at 13:58 flaeshflaesh 2621 gold badge5 silver badges17 bronze badges3 Answers
Reset to default 7Hi 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));
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.
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);