如何检查两个数组是否相等?

时间:2017-08-11 15:32:45

标签: javascript arrays

我试图比较两个数组是否相等。我打破了数组a,最后将它们存储到两个不同的数组bc中。最后,我正在检查控制台中的数组bc

控制台显示相同的值但是当我比较两个数组时,我得到的数组不相等。

这是我的代码:

var a = [1,2,3,4,3,2,1];
var b = [];
var c = [];
var t = 0;
var length = a.length;

console.log("is the array length" + length);
if (length %2 !== 0) {
    var mid = parseInt(length/2)-1;
    console.log(a[mid]);
    for(var j=length-1; j>(mid+1); j--) {
        c[t] = a[j];
        t++;
    }
    for(var i=0; i<=mid; i++) {
        b[i] = a[i];
    }
    console.log(c);
    console.log(b);

    if(b == c) { //comparing the array b and c
        console.log("true");
    }
    else {
        console.log("no")
    }
}

这是我的jsbin链接:https://jsbin.com/metexuruka/edit

2 个答案:

答案 0 :(得分:3)

取决于您对“相等”的定义 - 如果两个数组在同一位置包含相同的元素,则可以使用every

let areEqual = a.length === b.length && a.every((item, index) => b[index] === item);

如果您只想检查它们是否包含相同的元素,您仍然可以使用every,而无需进行索引检查:

let areEqual = a.length === b.length && a.every(item => b.indexOf(item) > -1);

答案 1 :(得分:0)

太长了,但你可以使用它:

<script type="text/javascript">
        /*$(function () {
            $(".nav-item").click(function () {
                $(".nav-item").each(function () {
                    $(this).find("a").removeClass("active");
                });
                $(this).find("a").addClass("active");
            });
        });*/

        // Warn if overriding existing method
        if (Array.prototype.equals)
            console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
        // attach the .equals method to Array's prototype to call it on any array
        Array.prototype.equals = function (array) {
            // if the other array is a falsy value, return
            if (!array)
                return false;

            // compare lengths - can save a lot of time 
            if (this.length != array.length)
                return false;

            for (var i = 0, l = this.length; i < l; i++) {
                // Check if we have nested arrays
                if (this[i] instanceof Array && array[i] instanceof Array) {
                    // recurse into the nested arrays
                    if (!this[i].equals(array[i]))
                        return false;
                }
                else if (this[i] != array[i]) {
                    // Warning - two different object instances will never be equal: {x:20} != {x:20}
                    return false;
                }
            }
            return true;
        }
        // Hide method from for-in loops
        Object.defineProperty(Array.prototype, "equals", { enumerable: false });

        var a = [1, 2, 3, 4, 3, 2, 1];
        var b = []; var c = []; var t = 0;
        var length = a.length;
        alert("is the array length" + length);
        if (length % 2 !== 0) {
            var mid = parseInt(length / 2) - 1;
            alert(a[mid]);
            for (var j = length - 1; j > (mid + 1) ; j--) {
                c[t] = a[j];
                t++;
            }
            for (var i = 0; i <= mid; i++) {
                b[i] = a[i];
            }
            alert(c);
            alert(b);

            if (b.equals(c)) { //comparing the array b and c
                alert("true");
            }
            else
                alert("no");
        }
    </script>