0

I'm trying to figure out a way to compare arrays in JavaScript, specifically in node.js .

I have 2 arrays :

Arr1:

[16,31,34,22,64,57,24,74,7,39,72,6,42,41,40,30,10,55,23,32,11,37,4,3,2,52,1,17,50,56,60,65,48,43,58,28,36,47,69,27,8,59,70,26,62,54,53,5,19,73]

Arr2

[12,11,9,14,7]

The idea is check if the values in Arr2 exist in Arr1, and, if they do, return true; any ideas?

0

3 Answers 3

6

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

Arr2.every( function( element ){ return Arr1.indexOf( element ) > - 1 })

Edit 2019: ES6 and Array.prototype.includes

Arr2.every( elm => Arr1.includes( elm ) )

In the comments @Pablo states that this will not work if the arrays contained objects. It is not entirely true, let me explain.

const ob1 = { foo: 1 }
const ob2 = { bar: 2 }
const ob3 = { baz: 3 }

const Arr1 = [ ob1, ob2, ob3 ];
const Arr2 = [ ob1, ob3 ];

Arr2.every( elm => Arr1.includes( elm ) ); // true, the same objects

const Arr3 = [ { foo: 1 }, { bar: 2 }, { baz: 3 } ]
const Arr4 = [ { foo: 1 }, { baz: 3 } ]

Arr4.every( elm => Arr3.includes( elm ) ); // false, distinct objects of the same "shape"

Neither of the above should come as a surprise to a JS developer, and I'd say this is the expected outcome. If you wanted to compare objects for sameness by their keys and values, then you could map them to JSON strings first:

const Arr3json = Arr3.map( elm => JSON.stringify( elm ) );
const Arr4json = Arr4.map( elm => JSON.stringify( elm ) );

Arr4json.every( elm => Arr3json.includes( elm ) );  // true, comparing string representations of distinct objects
5
  • 3
    Doh! I forgot about every. some works, but every is much clearer. Jan 15, 2014 at 15:10
  • 2
    Note that this is O(n*m). Jan 15, 2014 at 15:20
  • Great solution, i never tried .every() before, thanks for the suggestion. Jan 15, 2014 at 15:24
  • Please note that this code code doesn't works if at least one element in the array is an object, because objects are compared by reference, not by value. So object are only equal to themselves, If you compare two independent object the result will be false, no matter if both object are identical in properties and values.
    – Pablo
    May 3, 2019 at 16:33
  • @Pablo it does work with objects, question is if it's the expected outcome. See edited answer for explanation :)
    – pawel
    May 7, 2019 at 12:00
2

You can write your own function to do this:

var arr1 = [16,31,34,22,64,57,24];
var arr2 = [16,34,64,24];
var index;

for (var i = 0; i < arr2.length; i++) 
{
    index = arr1.indexOf(arr2[i]);
    if (index > -1) 
    {
        arr1.splice(index, 1);        
    }
}

This is just a help. From here you can try yourself to complete your programm.

0

Note that this code and @pawel code doesn't works if the elements of the array are object, because objects are compared by reference, not by value. So object are only equal to themselves, no matter if both object are identical in properties and values.

I propose an improvement to @pawel answer.

const Arr1 =  [16,31,34,22,64,57,24,74,7,39,72,6,42,41,40,30,10,55,23,32,11,37,4,3,2,52,1,17,50,56,60,65,48,43,58,28,36,47,69,27,8,59,70,26,62,54,53,5,19,73]
const Arr2 = [12,11,9,14,7]

const elementsInArr2ExistOnArr1 = Arr2.every( ( element ) => Arr1.includes(element))

console.log(`Elements in Arr2 exists on Arr1? ${elementsInArr2ExistOnArr1}`)

// Proof code works when elements are the same in both arrays

const Arr3 = [7,14,11,9,12]

const elementsInArr3ExistOnArr2 = Arr3.every( ( element ) => Arr2.includes(element))

console.log(`Elements in Arr3 existe on Arr2? ${elementsInArr3ExistOnArr2}`)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.