Javascript 比较两个数组并构建要删除的项和要添加的项的堆栈
我正在开发一个应用程序,现在需要比较两组数据 -标识不再出现在新数组中的项目数组 -标识数据流中新的项的数组 小提琴在这里- 这是一个数据示例Javascript 比较两个数组并构建要删除的项和要添加的项的堆栈,javascript,arrays,Javascript,Arrays,我正在开发一个应用程序,现在需要比较两组数据 -标识不再出现在新数组中的项目数组 -标识数据流中新的项的数组 小提琴在这里- 这是一个数据示例 var data = [ { "stream": [ { "userId": "isdskfk324023", "userName": "Melanie", "userAge":31,
var data = [
{
"stream": [
{
"userId": "isdskfk324023",
"userName": "Melanie",
"userAge":31,
"userState": "London",
"userCountry": "UK",
"userNotificationDate": "24/03/2014 11:00:23",
"userNotificationId": 2,
"userNotification": "Just came online",
"userImage": "http://www.gossip-gravy.com/wp-content/uploads/2013/08/GwenStefaniNoDoubt4.jpg",
"userLink": "#"
}
{
"userId": "pgflgf34534",
"userName": "Megan",
"userAge":28,
"userState": "London",
"userCountry": "UK",
"userNotificationDate": "24/03/2014 13:01:23",
"userNotificationId": 2,
"userNotification": "Just came online",
"userImage": "http://www.guysgab.com/wp-content/uploads/2013/07/Brittany-Mason-5.jpg",
"userLink": "#"
},
{
"userId": "gm3242323423",
"userName": "Gemma",
"userAge":32,
"userState": "London",
"userCountry": "UK",
"userNotificationDate": "24/03/2014 12:30:23",
"userNotificationId": 0,
"userNotification": "Just messaged you",
"userImage": "http://www.solveisraelsproblems.com/wp-content/uploads/2012/05/Esti-Ginzburg-18.jpg",
"userLink": "#"
}
]
},
{
"stream": [
{
"userId": "pgflgf34534",
"userName": "Megan",
"userAge":28,
"userState": "London",
"userCountry": "UK",
"userNotificationDate": "24/03/2014 13:01:23",
"userNotificationId": 0,
"userNotification": "Just messaged you",
"userImage": "http://www.guysgab.com/wp-content/uploads/2013/07/Brittany-Mason-5.jpg",
"userLink": "#"
},
{
"userId": "xcvxcvmxcdsf",
"userName": "Penolope",
"userAge":32,
"userState": "Texas",
"userCountry": "USA",
"userNotificationDate": "24/03/2014 12:00:23",
"userNotificationId": 2,
"userNotification": "Just came online",
"userImage": "http://zuqka.nation.co.ke/wp-content/uploads/2013/08/Lady-Gaga.jpg",
"userLink": "#"
}
]
}
]
我找到了解决这个问题的办法
function-byid(inArr,userId)
{
var exists=true;//数组中是否存在对象id
对于(i=0;i
我已尝试调整代码-根据此示例查找多余项您有什么问题?你被卡在哪一块了这似乎解决了问题-不确定这是否是最有效的方法-是否有人能够说明其他情况?^我想获取两个数据流-比较它们-返回新数据中不再显示的项目数组。将新项数组返回给此应用程序的旧数据它将使用这两个数组删除旧项,并插入新项
function pluckById(inArr, userId)
{
var exists = true;//does the object id exist in the array
for (i = 0; i < inArr.length; i++ )
{
if (inArr[i].userId == userId)
{
return (exists === true) ? true : inArr[i];
}
}
}
//__comparing two array sets and identify items no longer in new data stream
function removableItems(PreviousArr, CurrentArr){
var redundantItems = new Array();
//loop through the newDataSteam and look for items in oldDataStream
var CurrentArrSize = CurrentArr.length;
var PreviousArrSize = PreviousArr.length;
// loop through previous array
for(var j = 0; j < PreviousArrSize; j++) {
var inCurrentArray = pluckById(CurrentArr, PreviousArr[j].userId);
if(inCurrentArray == undefined){
redundantItems.push(PreviousArr[j]);//item no longer seen in new array
}
}
return redundantItems;
}
//__comparing two array sets and identify items no longer in new data stream
function insertableItems(PreviousArr, CurrentArr){
var newItems = new Array();
//loop through the newDataSteam and look for items new to oldDataStream
var CurrentArrSize = CurrentArr.length;
var PreviousArrSize = PreviousArr.length;
// loop through current array
for(var j = 0; j < CurrentArrSize; j++) {
var inPreviousArray = pluckById(PreviousArr, CurrentArr[j].userId);
if(inPreviousArray == undefined){
newItems.push(CurrentArr[j]);//item no longer seen in new array
}
}
return newItems;
}