比较两个对象数组

时间:2015-03-17 13:06:31

标签: javascript arrays json object comparison

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

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"}
]);

如何仅根据键进行比较而不查看对象的顺序?

3 个答案:

答案 0 :(得分:2)

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

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

我对上面的代码进行了更新。

答案 1 :(得分:1)

将对象序列化为字符串时,密钥不能保证按顺序排列。无论订单如何进行比较,请查看此Comparing two json arrays

答案 2 :(得分:1)

失败是因为 a 是一个数组,所以 sort(a)对数组索引排序 a 。你可以尝试:

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

它显示:

0,1

因此排序(a)排序不会将对象置于有意义的顺序中。它甚至不关心数组中的内容。

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