How to detect array equality in JavaScript?
13,026
Solution 1
- Check the length of both arrays
- Loop through the first array, compare each variable to the second array.
If 1
and 2
are both the same, your array is equal.
Function to compare objects/arrays:
Looping through true arrays can be achieved through for(var i=0; i<array.length; i++)
.
Walking through the properties of such an object can be done by for(var i in object)
.
function recursiveCompare(obj, reference){
if(obj === reference) return true;
if(obj.constructor !== reference.constructor) return false;
if(obj instanceof Array){
if(obj.length !== reference.length) return false;
for(var i=0, len=obj.length; i<len; i++){
if(typeof obj[i] == "object" && typeof reference[j] == "object"){
if(!recursiveCompare(obj[i], reference[i])) return false;
}
else if(obj[i] !== reference[i]) return false;
}
}
else {
var objListCounter = 0;
var refListCounter = 0;
for(var i in obj){
objListCounter++;
if(typeof obj[i] == "object" && typeof reference[i] == "object"){
if(!recursiveCompare(obj[i], reference[i])) return false;
}
else if(obj[i] !== reference[i]) return false;
}
for(var i in reference) refListCounter++;
if(objListCounter !== refListCounter) return false;
}
return true; //Every object and array is equal
}
If you don't understand the function, feel free to request an explanation at the comments.
Solution 2
With Javascript, you can't check if arrays are equals, but you can compare them like this:
var arr1 = ['alcohol', 'soft', 'hot'],
arr2 = ['apple', 'pear'],
arr3 = ['soft', 'hot', 'alcohol'];
function isSame(a1, a2){
return !(a1.sort() > a2.sort() || a1.sort() < a2.sort());
}
console.log( isSame(arr1, arr2) ); //false
console.log( isSame(arr1, arr3) ); //true
The sort
put all elements in the same order, and if both <
and >
comparisons are false it means both are the same.
Author by
David
Updated on July 20, 2022Comments
-
David almost 2 years
There are two arrays in JavaScript, they are both in the following format:
[{'drink':['alcohol', 'soft', 'hot']}, {'fruit':['apple', 'pear']}];
I need to detect if the two arrays are equal or not. they are considered equal if they contain the same elements in a different order. How can I make that?
-
Atticus over 12 yearsThis will work for you, just keep in mind order does matter here.
-
Narendra Yadala over 12 yearsYes. I was just updating the answer :)
-
Atticus over 12 yearsAll good :) Wasn't trying to rant on your response, it's a clever way to do it. Just wanted to make sure the OP knew this since he didn't specify
-
Rob W over 12 years@Sparky Good point. I've reversed the order of the notes. I've also updated my answer, and included a comparison function.
-
Sparky over 12 yearsThe benefits of being old and programming when languages were a lot slower...<wink>
-
Mic over 12 yearsDo you know you can compare arrays in Javascript? And know if they are bigger or smaller.
-
Rob W over 12 years@Mic What do you mean? Have you actually read the question + answer?
-
Mic over 12 years@Rob... I think so, did you check my answer below? Something wrong in it?
-
Rob W over 12 yearsAh I see. Your Array compare function also returns true for
isSame([1,2], [2,1])
. The sequence of the elements is often important. Also, I've just benchmarked our functions, and my function is three times faster: jsfiddle.net/Kv383/1. -
ArchNoob over 6 yearsIt gives me "compareArrays" is not a function.
-
Zack over 3 yearsThis isn't great because compareArrays is not a defined built-in function in Javascript and it's not great practice to much around with the prototype of something as fundamental as the Array class.