Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two arrays in node js?

I am having two arrays, how can i compare the two arrays at single shot.

   var arr1= ["a","b","c"];
   var arr2 = ["a","c","d"]

   if(arr1 == arr2){
      console.log(true);
    }else{
      console.log(false);
    }
like image 785
gauti Avatar asked Nov 23 '12 05:11

gauti


4 Answers

var arr1 = ["a","b","c"];
var arr2 = ["a","c","d"];

if (arr1.length == arr2.length
    && arr1.every(function(u, i) {
        return u === arr2[i];
    })
) {
   console.log(true);
} else {
   console.log(false);
}

Side note for edge cases:

=== is often considered slightly broken for this kind of task because NaN behaves unexpectedly:

var arr1 = ["a",NaN,"b"];
var arr2 = ["a",NaN,"b"];

if (arr1.length == arr2.length
    && arr1.every(function(u, i) {
        return u === arr2[i];
    })
) {
   console.log(true);
} else {
   console.log(false);
}

The code above actually logs false because NaN !== NaN. In addition, === can't distinguish +0 from -0. To cover both of these cases, you could use a stronger comparison known as "egal" or "is", which can easily be implemented like so:

function is(a, b) {
    return a === b && (a !== 0 || 1 / a === 1 / b) // false for +0 vs -0
        || a !== a && b !== b; // true for NaN vs NaN
}

var arr1 = ["a",NaN,"b"];
var arr2 = ["a",NaN,"b"];

if (arr1.length == arr2.length
    && arr1.every(function(u, i) {
        // Use "is" instead of "==="
        return is(u, arr2[i]);
    })
) {
   console.log(true);
} else {
   console.log(false);
}
like image 163
Nathan Wall Avatar answered Sep 21 '22 10:09

Nathan Wall


The top answer is good, but I would also consider using Array.prototype:

Array.prototype.equals = function (arr) {
    return this.length == arr.length && this.every((u, i) => u === arr[i]);
}

console.log([1,2,3].equals([1,2,3])); // true
console.log([1,2,3].equals([1,3,3])); // false
// BUT!
console.log(["a",NaN,"b"].equals(["a",NaN,"b"])); // false, because NaN !== NaN

If you want it to work for NaNs too and distinguish +0 and -0, better use this:

Array.prototype.equals = function (arr) {
    function is(a, b) { // taken from the top answer
        return a === b && (a !== 0 || 1 / a === 1 / b) // false for +0 vs -0
            || a !== a && b !== b; // true for NaN vs NaN
    }
    return this.length == arr.length && this.every((u, i) => is(u, arr[i]));
}

console.log(["a",NaN,"b"].equals(["a",NaN,"b"])); // true
like image 20
ibodi Avatar answered Sep 19 '22 10:09

ibodi


[ES6]

Top answer is good & enough.

But when you just want to compare its values are same you have to sort it before. here's no need sort code.

if(arr1.length == arr2.length && arr1.every((v) => arr2.indexOf(v) >= 0)) {
    console.log(true);
} else {
    console.log(false);
}

And.. I think using a 'some' instead of 'every' is better.

If those are not same, 'some' gives you a early exit. - very little early but early ;)

if(arr1.length == arr2.length && !arr1.some((v) => arr2.indexOf(v) < 0)) {
    console.log(true);
} else {
    console.log(false);
}
like image 39
Taihwan Hah Avatar answered Sep 21 '22 10:09

Taihwan Hah


I wanted to add some modification of the code made by 'Taihwan Hah' but could not leave a comment (the system told me so)

So here is my modifs:

function ArrayEquals(arr1,arr2){
    return arr1.length === arr2.length && !arr1.some((v) => arr2.indexOf(v) < 0) && !arr2.some((v) => arr1.indexOf(v) < 0);
}

basically, I had to check for but array because my arrays do not contains unique numbers.

like image 37
LeRoss Avatar answered Sep 20 '22 10:09

LeRoss