比较两个对象数组

Comparing two array of objects

我正在尝试比较两个对象数组。下面是我的代码。

var result = identical([
    {"depid": "100", "depname": ""},
    {"city": "abc", "state": "xyz"},
    {"firstName": "John", "lastName": "Doe", "contactno": {"ph": 12345, "mob": 485428428}}
], [
    {"firstName": "John", "lastName": "Doe", "contactno": {"ph": 12345, "mob": 485428428}},
    {"depid": "100", "depname": ""},
    {"city": "abc", "state": "xyz"}
]);

console.log(result); // returns false

function identical(a, b) {
    function sort(object) {
      if (typeof object !== "object" || object === null) {
            return object;
        }
        return Object.keys(object).sort().map(function (key) {
            return {
                key: key,
                value: sort(object[key])
            };
        });
    }

    return JSON.stringify(sort(a)) === JSON.stringify(sort(b));
};

我想知道为什么在比较上述两个对象数组时得到的结果为假。

如果我传递以下对象,结果为真

var result = identical([
    {"firstName": "John", "lastName": "Doe", "contactno": {"ph": 12345, "mob": 485428428}},
    {"depid": "100", "depname": ""},
    {"city": "abc", "state": "xyz"}
], [
    {"firstName": "John", "lastName": "Doe", "contactno": {"ph": 12345, "mob": 485428428}},
    {"depid": "100", "depname": ""},
    {"city": "abc", "state": "xyz"}
]);

如何在不看对象顺序的情况下仅根据键进行比较?

在将对象序列化为字符串时,不保证键的顺序相同。要不考虑顺序进行比较,请检查此 Comparing two json arrays

失败是因为a是数组,所以sort(a)a排序关于数组索引。你可以试试:

 var l = ['a','b'];
 alert(Object.keys(l));

显示:

0,1

所以 sort(a) 排序不会将对象按有意义的顺序放入其中。它甚至不关心数组中的内容。

如果您想比较对象数组,我建议您对数组的每个对象使用函数排序,然后对数组中的每个对象进行 jsonify,然后对字符串数组进行排序并比较两个排序后的数组字符串。

我找到的解决方案之一是定义一个函数来测试对象的相等性,然后您需要为数组中的每个元素调用该函数。这段代码对我来说很好用:

Object.prototype.equals = function(x) {
        for(p in this) {
            switch(typeof(this[p])) {
                    case 'object':
                            if (!this[p].equals(x[p])) { return false }; break;
                    case 'function':
                            if (typeof(x[p])=='undefined' || (p != 'equals' && this[p].toString() != x[p].toString())) { return false; }; break;
                    default:
                            if (this[p] != x[p]) { return false; }
            }
        }
        for(p in x) {
            if(typeof(this[p])=='undefined') {return false;}
        }

        return true;
    }

来源:Object comparison in JavaScript

    function identical (arr1, arr2) {

        if(arr1.length != arr2.length) {
            return false;
        }

        var exists = arr1.length;
        for(var i = 0; i<arr1.length; i++) {
            for(var j =0; j<arr2.length ; j++) {
                if(Object.keys(arr1[i]).equals(Object.keys(arr2[j]))) {
                   exists--;
                }
            }
        }

        return !exists;
    }

现在这段代码的结果是true

var result = identical([
    {"firstName": "John", "lastName": "Doe", "contactno": {"ph": 12345, "mob": 485428428}},
    {"depid": "100", "depname": ""},
    {"city": "abc", "state": "xyz"}
], [
    {"firstName": "John", "lastName": "Doe", "contactno": {"ph": 12345, "mob": 485428428}},
    {"depid": "100", "depname": ""},
    {"city": "abc", "state": "xyz"}
]);

编辑:

要仅比较键,您需要使用:

Object.keys(arr1[i]).equals(Object.keys(arr2[j])

而不是

arr1[i].equals(arr2[j])

我更新了上面的代码。