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

发布于 2024-12-07 03:55:07 字数 188 浏览 1 评论 0 原文

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

[{'drink':['alcohol', 'soft', 'hot']}, {'fruit':['apple', 'pear']}];

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏の忆 2024-12-14 03:55:07
  1. 检查两个数组的长度
  2. 循环第一个数组,将每个变量与第二个数组进行比较。

如果 12 相同,则数组相等。

比较对象/数组的函数:

通过for(var i=0; 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
}

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

  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.

羁客 2024-12-14 03:55:07

使用 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

sort 将所有元素按相同顺序排列,如果都 <> 比较为 false,这意味着两者相同。

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.

半仙 2024-12-14 03:55:07

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

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

深陷 2024-12-14 03:55:07

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

var equal = array1.compareArrays(array2);

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

var equal = array1.compareArrays(array2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文