Compare arrays in JavaScript

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?

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

Compare Arrays in JavaScript, With that in mind, here's 3 definitions of equality for arrays and how to check them . Same Length, Each Value Equal. One approach for comparing� We have 2 arrays in JavaScript and we want to compare one with the other to see if the elements of master array exists in keys array, and then make one new array of the same length that of the master array but containing only true and false (being true for the values that exists in keys array and false the ones that don't).

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.

How to compare two arrays in JavaScript?, In Javascript, to compare two arrays we need to check that the length of both arrays should be same, the objects present in it are of the same type and each item in one array is equal to the counterpart in another array. By doing this we can conclude both arrays are the same or not. JavaScript provides a function JSON. To compare two Arraysin JavaScript, you should check that the lengthof both arrays should be the same, the objectspresented in it be the same type, and each item in one array is equivalent to the counterpart in the compared array. This tutorial will show you some ways of comparing two arrays.

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}`)

Comparison of Two arrays Using JavaScript, Comparison of arrays by using forEach method. This question is most commonly asked in interviews. There are different ways to achieve these,� To answer the original question, one way you could do this, and mind you, this is a bit of a hack, simply serialize the two arrays to JSON and then compare the two JSON strings. That would simply tell you if the arrays are different, obviously you could do this to each of the objects within the arrays as well to see which ones were different.

Check if two arrays or objects are equal with JavaScript, What we need to compare #. You could have a simple array, like this one. var arr = [1� The Difference Between Arrays and Objects In JavaScript, arrays use numbered indexes. In JavaScript, objects use named indexes. Arrays are a special kind of objects, with numbered indexes.

Compare Arrays in JavaScript, In this post, we will see how to compare arrays of any depth in JavaScript. Two arrays are said to be equal if they have same elements in the same order. JavaScript automatically converts an array to a comma separated string when a primitive value is expected. This is always the case when you try to output an array. These two examples will produce the same result:

JavaScript array: Find the difference of two arrays, JavaScript exercises, practice and solution : Write a JavaScript function to find the difference of two arrays.

Comments
  • Doh! I forgot about every. some works, but every is much clearer.
  • Note that this is O(n*m).
  • Great solution, i never tried .every() before, thanks for the suggestion.
  • 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 it does work with objects, question is if it's the expected outcome. See edited answer for explanation :)