最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to detect array equality in JavaScript? - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Dec 30, 2014 at 12:09 Mark Walters 12.4k6 gold badges35 silver badges48 bronze badges asked Sep 29, 2011 at 6:59 DavidDavid 2,7218 gold badges40 silver badges53 bronze badges 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? – Sparky 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) – 6502 Commented Sep 29, 2011 at 7:13
  • Is case important, i.e. should pear and PEAR be considered the same? – Sparky Commented Sep 29, 2011 at 7:18
  • possible duplicate of how to check javascript array equals? – Felix Kling Commented Sep 29, 2011 at 8:04
  • possible duplicate of How to know if two arrays have the same values – Palec Commented Jun 24, 2014 at 14:35
Add a ment  | 

4 Answers 4

Reset to default 6
  1. Check the length of both arrays
  2. Loop through the first array, pare each variable to the second array.

If 1 and 2 are both the same, your array is equal.

Function to pare 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 ments.

With Javascript, you can't check if arrays are equals, but you can pare 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 > parisons are false it means both are the same.

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

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

var equal = array1.pareArrays(array2);
发布评论

评论列表(0)

  1. 暂无评论