如何检测 JavaScript 中的数组相等性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7593590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:38:17  来源:igfitidea点击:

How to detect array equality in JavaScript?

javascriptarraysstringequality

提问by David

There are two arrays in JavaScript, they are both in the following format:

JavaScript 中有两个数组,它们的格式如下:

[{'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?

我需要检测两个数组是否相等。如果它们以不同的顺序包含相同的元素,则它们被认为是相等的。我怎样才能做到这一点?

回答by Rob W

  1. Check the length of both arrays
  2. Loop through the first array, compare each variable to the second array.
  1. 检查两个数组的长度
  2. 循环遍历第一个数组,将每个变量与第二个数组进行比较。

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

如果12都相同,则您的数组相等。

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).

循环遍历真正的数组可以通过for(var i=0; i<array.length; i++).
遍历此类对象的属性可以通过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.

如果您不了解该功能,请随时在评论中请求解释。

回答by JavaJens

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

这回答了你的问题了吗? 如何检查两个数组是否与 JavaScript 相等?

var equal = array1.compareArrays(array2);

回答by Mark Walters

http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256BFB0077DFFD

http://www.breakpar.com/bkp/home.nsf/0/87256B280015193F87256BFB0077DFFD

Have a look here, the second example is a function that does exactly that.

看看这里,第二个例子是一个完全这样做的函数。

A similar function that does the same thing can be found at the bottom of this page.

可以在本页底部找到执行相同操作的类似函数。

http://zeeohemgee.blogspot.com/2006/07/comparing-and-copying-arrays-in.html

http://zeeohemgee.blogspot.com/2006/07/comparing-and-copying-arrays-in.html

Hope this helps

希望这可以帮助

Cheers

干杯

回答by Narendra Yadala

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

JSON.stringify(array1)===JSON.stringify(array2);如果您希望两个数组中的顺序也相同,则可以尝试此操作。

回答by Mic

With Javascript, you can't check if arrays are equals, but you can compare them like this:

使用 Javascript,您无法检查数组是否相等,但您可以像这样比较它们:

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 sortput all elements in the same order, and if both <and >comparisons are false it means both are the same.

sort放在同一订单中的所有元素,如果两个<>比较是错误的意思都是一样的。