How to find elements in array2 that are not in array1?

£可爱£侵袭症+ 提交于 2019-12-20 06:33:26

问题


I have two arrays:

var a1 = [ { ID: 2, N:0 }, { ID: 1, N:0 } ];
var a2 = [ { ID: 1, N:0 }, { ID: 2, N:0 }, { ID: 3, N:0 } ];

I need to get all elements that are on a2 but not in a1. An element here is distinct of another only by the property ID, the other properties should be ignored. And I cannot guarantee the order of the elements on the arrays. Meaning the result for this example should be:

var result = [ { ID: 3, N:0 } ]; // result for the example above

How can I do this in an efficient way? (I will be comparing arrays from 500 to 5,000 length)


回答1:


To do this efficiently, you need to build an index of the items that are already in a1 so you can cycle through a2 and compare each one to the index to see if it's already been seen or not. One can use a javascript object for an index. Cycle through a1 and put all its IDs into the index. Then cycle through a2 and collect any items whose ID does not appear in the index.

function findUniques(testItems, baseItems) {
    var index = {}, i;
    var result = [];

    // put baseItems id values into the index
    for (i = 0; i < baseItems.length; i++) {
        index[baseItems[i].ID] = true;
    }

    // now go through the testItems and collect the items in it 
    // that are not in the index
    for (i = 0; i < testItems.length; i++) {
        if (!(testItems[i].ID in index)) {
            result.push(testItems[i]);
        }
    }
    return(result);
}

var a1 = [ { ID: 2, N:0 }, { ID: 1, N:0 } ];
var a2 = [ { ID: 1, N:0 }, { ID: 2, N:0 }, { ID: 3, N:0 } ];

var result = findUniques(a2, a1);
// [{"ID":3,"N":0}]

Working demo: http://jsfiddle.net/jfriend00/uDEtg/




回答2:


The same question has been posted a few times, have a look here:

JavaScript array difference

Most solutions are given through 'native' javascript however. I sometimes prefer to use underscore.js, since I build a lot of things using backbone.js and underscore is a dependency for Backbone. So I can use its awesome utilities. You might consider loading them in:

http://documentcloud.github.com/underscore/

var a1 = [ { ID: 2, N:0 }, { ID: 1, N:0 } ];
var a2 = [ { ID: 1, N:0 }, { ID: 2, N:0 }, { ID: 3, N:0 } ];

var from, to;
if(a1 > a2){
    from = a1
    to = a2
} else {
    from = a2
    to = a1
}

var a3 = _.filter(from, function(obj){
    var compare = _.find(to, function(obj2){ return obj.ID === obj2.ID });
    return compare === undefined
});
console.log(a3);

I first determined the longest array, I do this because I want to compare as many objects as possible to the shorter list. Otherwise we'd 'forget' some.

Then I simply use filter and find in the underscore.js library to return the objects that aren't in the shorter array, but ARE in the longer array.

If both arrays are of equal length it is fine too, because then we'd compare all of the items to all of the others.



来源:https://stackoverflow.com/questions/10048766/how-to-find-elements-in-array2-that-are-not-in-array1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!