比较两个数组对象 [英] Comparing two array of objects
问题描述
我想比较两个数组对象。下面是我的code。
I am trying to compare two array of objects. Below is my code.
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));
};
我想知道为什么我收到的结果为假,而对比上述两个数组对象。
I want to know why I am getting result as false while comparing the above two array of objects.
如果我通过下面的对象,其结果是真实的。
If I pass the below object, the result is 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"}
]);
如何基于单独键和没有看到对象的顺序比较?
How to compare based on keys alone and without seeing the order of objects ?
推荐答案
一我找到了解决方案,是定义一个函数来测试对象的平等,那么你需要调用该函数用于对每一个元素数组。这code正常工作对我来说:
One of the solutions I found, is to define a function to test the equality of the objects, and then you need to call that function for each element on the array. this code works fine for me:
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;
}
来源:<一个href=\"http://stackoverflow.com/questions/1068834/object-comparison-in-javascript#answer-1068883\">Object在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;
}
现在这个code的结果是真正
Now the result of this code is 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"}
]);
编辑:
要比较只有按键,你需要使用:
To compare only keys, you need to use :
Object.keys(arr1[i]).equals(Object.keys(arr2[j])
而不是
arr1[i].equals(arr2[j])
我做的code以上的更新。
I made the update on the code above.
这篇关于比较两个数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!