在Javascript中比较两个对象数组并根据id更新lastSeen

问题描述 投票:1回答:4

我有两个用户对象数组,其中包含_id和lastSeen。

allUsersSeen = [
   {_id: '12csdf', lastSeen: 212512},
   {_id: '232csdf', lastSeen: 34345},
   {_id: '1253csdf', lastSeen: 6346453},
   {_id: '772csfddf', lastSeen: 2345345}
]

currentUsersSeen = [
   {_id: '12csdf', lastSeen: Date.now()},
   {_id: '1253csdf', lastSeen: Date.now()},
   {_id: '1fsdf', lastSeen: Date.now()}
]

我想比较这两个数组,如果allUsersSeen _id与currentUsersSeen匹配,我想将lastSeen更新为Date.now()。所有其他_id将使其lastSeen保持原样。

应该将来自currentUsersSeen的任何剩余_id推送到allUsersSeen上,并使用lastSeen of Date.now()

allUsersSeen的最终数组应如下所示:

allUsersSeen = [
   {_id: '12csdf', lastSeen: Date.now()},
   {_id: '232csdf', lastSeen: 34345},
   {_id: '1253csdf', lastSeen: Date.now()},
   {_id: '772csfddf', lastSeen: 2345345},
   {_id: '1fsdf', lastSeen: Date.now()}
]

显然,Date.now()将是当前的日期/时间。我该怎么做呢?我尝试过使用嵌套的for循环,并尝试使用forEach()和map()。

任何帮助将不胜感激。谢谢。

javascript arrays node.js mongodb object
4个回答
0
投票

试试这样,

allUsersSeen = [
        {_id: '12csdf', lastSeen: 212512},
        {_id: '232csdf', lastSeen: 34345},
        {_id: '1253csdf', lastSeen: 6346453},
        {_id: '772csfddf', lastSeen: 2345345}
    ];

    currentUsersSeen = [
        {_id: '12csdf', lastSeen: Date.now()},
        {_id: '1253csdf', lastSeen: Date.now()},
        {_id: '1fsdf', lastSeen: Date.now()}
    ];

    function merge(a, b, prop){
        var reduced =  a.filter( a_item => ! b.find ( b_item => a_item[prop] === b_item[prop]) )
        return reduced.concat(b);
    }
    console.log( merge(allUsersSeen, currentUsersSeen, "_id") );

0
投票

如果你明白你想要做什么,你可以通过首先从allUsersSeen(如果存在)中删除元素然后再从currentUser添加它来实现。

allUsersSeen = [
   {_id: '12csdf', lastSeen: 212512},
   {_id: '232csdf', lastSeen: 34345},
   {_id: '1253csdf', lastSeen: 6346453},
   {_id: '772csfddf', lastSeen: 2345345}
]

currentUsersSeen = [
   {_id: '12csdf', lastSeen: Date.now()},
   {_id: '1253csdf', lastSeen: Date.now()},
   {_id: '1fsdf', lastSeen: Date.now()}
]

currentUsersSeen.forEach(user => {
  allUsersSeen = allUsersSeen.filter( obj => obj._id != user.id )
  allUsersSeen.push(user)
})

0
投票

你可以array#concat两个阵列,然后使用array#reduce来更新lastSeen

var allUsersSeen = [{_id: '12csdf', lastSeen: 212512},{_id: '232csdf', lastSeen: 34345},{_id: '1253csdf', lastSeen: 6346453},{_id: '772csfddf', lastSeen: 2345345}],
    currentUsersSeen = [{_id: '12csdf', lastSeen: Date.now()},{_id: '1253csdf', lastSeen: Date.now()},{_id: '1fsdf', lastSeen: Date.now()}],
    
    result = Object.values( allUsersSeen.concat(currentUsersSeen).reduce((r, {_id, lastSeen}) => {
      r[_id] = r[_id] ? {...r[_id], lastSeen} : {_id, lastSeen};
      return r;
    },{}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

0
投票

你可能想要这样的东西

allUsersSeen = [
   {_id: '12csdf', lastSeen: 212512},
   {_id: '232csdf', lastSeen: 34345},
   {_id: '1253csdf', lastSeen: 6346453},
   {_id: '772csfddf', lastSeen: 2345345}
]

currentUsersSeen = [
   {_id: '12csdf', lastSeen: Date.now()},
   {_id: '1253csdf', lastSeen: Date.now()},
   {_id: '1fsdf', lastSeen: Date.now()}
]

let newArr = allUsersSeen.map(user => currentUsersSeen.find(curr_user => curr_user._id === user._id) || user);
© www.soinside.com 2019 - 2024. All rights reserved.