10

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?

Mark Walters
12.4k6 gold badges36 silver badges48 bronze badges
asked Sep 29, 2011 at 6:59
5
  • Would you consider them equal if they contain the same elements in a different order, or do they have to be identical to both content and order? Is case important? Commented Sep 29, 2011 at 7:04
  • Do you mean also checking different ordering recursively? If second array is [{'fruit':['pear', 'apple']}, {'drink':['alcohol', 'hot', 'soft']}] as elements is considered equal to the array you've shown? (note the pear/apple exchange) Commented Sep 29, 2011 at 7:13
  • Is case important, i.e. should pear and PEAR be considered the same? Commented Sep 29, 2011 at 7:18
  • possible duplicate of how to check javascript array equals? Commented Sep 29, 2011 at 8:04
  • possible duplicate of How to know if two arrays have the same values Commented Jun 24, 2014 at 14:35

4 Answers 4

6
  1. Check the length of both arrays
  2. 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.

answered Sep 29, 2011 at 7:07
Sign up to request clarification or add additional context in comments.

6 Comments

@Sparky Good point. I've reversed the order of the notes. I've also updated my answer, and included a comparison function.
The benefits of being old and programming when languages were a lot slower...<wink>
Do you know you can compare arrays in Javascript? And know if they are bigger or smaller.
@Mic What do you mean? Have you actually read the question + answer?
@Rob... I think so, did you check my answer below? Something wrong in it?
|
1

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.

answered Sep 29, 2011 at 9:18

Comments

0

You can try this JSON.stringify(array1)===JSON.stringify(array2); if you want the order also to be identical in both the arrays.

answered Sep 29, 2011 at 7:06

3 Comments

This will work for you, just keep in mind order does matter here.
Yes. I was just updating the answer :)
All 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
-1

Does this answer your question? How to check if two arrays are equal with JavaScript?

var equal = array1.compareArrays(array2);
answered Sep 29, 2011 at 7:05

2 Comments

It gives me "compareArrays" is not a function.
This 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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.