javascript - Filter unique objects in array based on timestamp condition - Stack Overflow

I have the following array:let arr = [{"id": 123, "lastUpdate": 1543229793},{"

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 badges
Add a ment  | 

3 Answers 3

Reset to default 7

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));

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745339184a4623246.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信