使用javascript找到两个对象数组的差异

时间:2022-12-10 11:35:17

I have two arrays that contain a list of "message" objects. Each message object has a property 'receivedTime'.

我有两个包含“消息”对象列表的数组。每个消息对象都有一个属性'receivedTime'。

I can sort the two arrays by 'receivedTime' so that newest messages are first.

我可以通过'receivedTime'对两个数组进行排序,以便最先发布消息。

Now, using underscorejs (if applicable)...is there a way I can pop off the difference in the two arrays? The newer array will have newer messages than the original list.

现在,使用underscorejs(如果适用)...有没有办法可以突破两个数组的差异?较新的数组将具有比原始列表更新的消息。

I only want new messages that are not in the original array.

我只想要不在原始数组中的新消息。

For some context, I can only get all messages from the API. so I need to keep doing that in a timer, and add any new messages to the table on the page (this part I can do, I just need the difference in the list from one call to the next).

对于某些上下文,我只能从API获取所有消息。所以我需要在计时器中继续这样做,并将任何新消息添加到页面上的表中(这部分我可以做,我只需要从一次调用到下一次调用的列表中的区别)。

4 个解决方案

#1


2  

What is format of recievedTime ? if it's timestamp then , How about saving last recievedTime and querying list to filter only new ones.

什么是recievedTime的格式?如果它是时间戳,那么如何保存最后收到的时间和查询列表以仅过滤新的。

You can use pure javascript

你可以使用纯JavaScript

var newones = items.filter( function(item){return (item.recievedTime>lastRecievedTime);} );

#2


0  

After sorting both arrays, pass them to for loop and check if value is in second array.

对两个数组进行排序后,将它们传递给for循环并检查value是否在第二个数组中。

for(var i = 0; i < newMessages.length; i++) {
    if(Messages[i] == newMessages[i]) {
         newMessages.splice(i, 1);
    }
}

Else find length of Messages and remove that number of elements present in newMessages array.

否则找到消息的长度并删除newMessages数组中存在的元素数量。

#3


0  

Try using filter and then specifying a function that returns only those not in the original array, something along the lines of this:

尝试使用过滤器,然后指定一个只返回原始数组中不存在的函数的函数,类似于以下内容:

_.filter(newer_array, function(message) {
    return _.contains(older_array,message); 
});

#4


0  

Or, another option would be.. after sorting both arrays, find the receivedTime of the newest message in the "older" array, and then iterate through the "newer" array until you get to the receivedTime found earlier. Something along these lines...

或者,另一个选项是......在对两个数组进行排序后,在“较旧”数组中找到最新消息的receivedTime,然后遍历“较新”数组,直到找到之前找到的receiveTime。沿着这些方向......

var split = older[0].receivedTime
var difference = []

var i = 0
while(newer[i].receivedTime !== split) {
    difference.push(newer[i]);
}

#1


2  

What is format of recievedTime ? if it's timestamp then , How about saving last recievedTime and querying list to filter only new ones.

什么是recievedTime的格式?如果它是时间戳,那么如何保存最后收到的时间和查询列表以仅过滤新的。

You can use pure javascript

你可以使用纯JavaScript

var newones = items.filter( function(item){return (item.recievedTime>lastRecievedTime);} );

#2


0  

After sorting both arrays, pass them to for loop and check if value is in second array.

对两个数组进行排序后,将它们传递给for循环并检查value是否在第二个数组中。

for(var i = 0; i < newMessages.length; i++) {
    if(Messages[i] == newMessages[i]) {
         newMessages.splice(i, 1);
    }
}

Else find length of Messages and remove that number of elements present in newMessages array.

否则找到消息的长度并删除newMessages数组中存在的元素数量。

#3


0  

Try using filter and then specifying a function that returns only those not in the original array, something along the lines of this:

尝试使用过滤器,然后指定一个只返回原始数组中不存在的函数的函数,类似于以下内容:

_.filter(newer_array, function(message) {
    return _.contains(older_array,message); 
});

#4


0  

Or, another option would be.. after sorting both arrays, find the receivedTime of the newest message in the "older" array, and then iterate through the "newer" array until you get to the receivedTime found earlier. Something along these lines...

或者,另一个选项是......在对两个数组进行排序后,在“较旧”数组中找到最新消息的receivedTime,然后遍历“较新”数组,直到找到之前找到的receiveTime。沿着这些方向......

var split = older[0].receivedTime
var difference = []

var i = 0
while(newer[i].receivedTime !== split) {
    difference.push(newer[i]);
}