majidarif
majidarif

Reputation: 20045

Get difference of 2 arrays but also get which specific side

Let us say I have 2 arrays:

["hello", "world"] vs ["hello", "earth"]

ArrayA has a difference of earth, where as ArrayB has a difference of world.

How would I know this?

Currently I am using underscores difference method.

diff = _.difference(ArrayA, ArrayB)

But this will only return world, earth and I do not know which one of the 2 arrays is missing them.

Or is there a better approach to this?

Upvotes: 0

Views: 30

Answers (1)

juvian
juvian

Reputation: 16068

Try this:

var arr1=["hello", "world"];
var arr2=["hello", "earth"];

var diff = _.difference(ArrayA, ArrayB);

for(var i=0;i<diff.lenght;i++){
    if(arr1.indexOf(diff[i]) != -1){
        //arr2 doesnt have it
    }else{
        //arr1 doesnt have it
    }
}

Upvotes: 2

Related Questions