admin管理员组文章数量:1420973
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);
本文标签: javascriptFilter unique objects in array based on timestamp conditionStack Overflow
版权声明:本文标题:javascript - Filter unique objects in array based on timestamp condition - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745339184a2654181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论