简体   繁体   中英

Compare two arrays and build a stack of items to remove and items to add

I am working on an application, which now requires me to compare two sets of data...

-identify an array of items that no longer appear in the new array -identify an array of items that are new to the data stream

the fiddle is here - http://jsfiddle.net/CXqM2/129/

here is a sample of the data

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

I found a solution to this problem

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM