I have two array with 5 objects each. Before I proceed, I want to check whether these two arrays are equal or not. I tried looking for the answers but unfortunately couldnt find anything for nested arrays with multiple objects. Is there a way to achieve this?
我有两个数组,每个数组有5个对象。在继续之前,我想检查这两个数组是否相等。我试着寻找答案,但遗憾的是无法找到任何嵌套数组有多个对象的东西。有没有办法实现这个目标?
eg. Array1 ==> 5 nested objects Array2 ==> 5 nested objects
例如。 Array1 ==> 5个嵌套对象Array2 ==> 5个嵌套对象
Now check whether Array1 == Array2 and return a boolean value.
现在检查Array1 == Array2是否返回一个布尔值。
2 个解决方案
#1
2
You can do it without jQuery - change them to a string by using globally available JSON.stringify
method, then the comparison will be easy:
你可以在没有jQuery的情况下完成它 - 通过使用全局可用的JSON.stringify方法将它们更改为字符串,然后比较将很容易:
JSON.stringify(arr1) === JSON.stringify(arr2);
This is kind of a hack. But it does work well. And in the era when Angular framework is checking it's injections by running toString()
on its functions and then regexping the attributes (oh yes it does), I think this is just an effective solution ;)
这是一种黑客行为。但它确实运作良好。在Angular框架通过在其函数上运行toString()然后对属性进行重新定位来检查它的注入的时代(哦,是的),我认为这只是一个有效的解决方案;)
#2
0
var arr1 = [{val: 1}, {val: 2}, {val: 3}, {val: 4}, {val: 5}]
var arr2 = [{val: 1}, {val: 2}, {val: 3}, {val: 4}, {val: 5}]
var arr3 = [{val: 1}, {val: 2}, {val: 3}, {val: 9}, {val: 5}]
function isEqual(arr1, arr2) {
// If the array lengths are different, return false
if (arr1.length !== arr2.length) return false;
// Grab an array of true/false values determined
// by the result of the `some` callback
// Return false if `false` is found in the array, otherwise true
return arr1.map(function (one) {
return arr2.some(function (two) {
return two.val === one.val;
});
}).indexOf(false) > -1 ? false : true;
}
isEqual(arr1, arr2); // true
isEqual(arr1, arr3); // false
#1
2
You can do it without jQuery - change them to a string by using globally available JSON.stringify
method, then the comparison will be easy:
你可以在没有jQuery的情况下完成它 - 通过使用全局可用的JSON.stringify方法将它们更改为字符串,然后比较将很容易:
JSON.stringify(arr1) === JSON.stringify(arr2);
This is kind of a hack. But it does work well. And in the era when Angular framework is checking it's injections by running toString()
on its functions and then regexping the attributes (oh yes it does), I think this is just an effective solution ;)
这是一种黑客行为。但它确实运作良好。在Angular框架通过在其函数上运行toString()然后对属性进行重新定位来检查它的注入的时代(哦,是的),我认为这只是一个有效的解决方案;)
#2
0
var arr1 = [{val: 1}, {val: 2}, {val: 3}, {val: 4}, {val: 5}]
var arr2 = [{val: 1}, {val: 2}, {val: 3}, {val: 4}, {val: 5}]
var arr3 = [{val: 1}, {val: 2}, {val: 3}, {val: 9}, {val: 5}]
function isEqual(arr1, arr2) {
// If the array lengths are different, return false
if (arr1.length !== arr2.length) return false;
// Grab an array of true/false values determined
// by the result of the `some` callback
// Return false if `false` is found in the array, otherwise true
return arr1.map(function (one) {
return arr2.some(function (two) {
return two.val === one.val;
});
}).indexOf(false) > -1 ? false : true;
}
isEqual(arr1, arr2); // true
isEqual(arr1, arr3); // false