比较两个数组并构建要删除的项目堆栈和要添加的项目

时间:2014-03-25 11:04:37

标签: javascript arrays

我正在开发一个应用程序,现在需要我比较两组数据......

- 标识不再出现在新数组中的项目数组 - 标识数据流的新项目数组

小提琴在这里 - http://jsfiddle.net/CXqM2/129/

这是一个数据样本

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": "#"
            }
        ]
    }
]

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法

http://jsfiddle.net/CXqM2/142/

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