[英]Efficient way to compare arrays in javascript
I need to compare the elements from two arrays as follows: 我需要比较两个数组中的元素,如下所示:
arr1[0] ? arr2[0]
arr1[1] ? arr2[1]
arr1[2] ? arr2[2]
etc.
I wrote some code but it seems to be slow when I try to compare 1000 objects like this on each array : 我写了一些代码,但是当我尝试在每个数组上比较1000个对象时,它似乎很慢:
{
"id":"event707",
"name":"Custom707",
"type":"disabled",
"default_metric":false,
"participation":"disabled",
"serialization":"always_record"
}
This is how my function looks like (just an example for two arrays with hard coded data). 这就是我的函数的样子(仅是两个带有硬编码数据的数组的示例)。
function compare() {
var step = 0;
var fruits1 = [{"apple":25},{"bannana":36},{"orange":6}];
var fruits2 = [{"apple":25},{"bannana":36},{"orange":6}];
for(var i=0;i<fruits1.length;i++) {
for(var j=step;j<fruits2.length;j++) {
console.log("FRUIT1");
console.log(JSON.stringify(fruits1[i]));
console.log("FRUIT2");
console.log(JSON.stringify(fruits2[j]));
console.log("----------------------");
if(JSON.stringify(fruits1[i])!== JSON.stringify(fruits2[j])) {
//do something
}
step = step + 1;
break;
}
}
}
Simple function without library: 没有库的简单功能:
var arr1 = [1,2,3]; var arr2 = [1,2,4]; //This function takes one item, the index of the item, and another array to compare the item with. function compare(item, index, array2){ return array2[index] == item; } // the forEach method gives the item as first parameter // the index as second parameter // and the array as third parameter. All are optional. arr1.forEach(function(item, index){ console.log(compare(item, index, arr2)); });
Combine this with the answer Abdennour TOUMI
gave, and you have an object comparison method :) 将其与Abdennour TOUMI
给出的答案Abdennour TOUMI
,您就有了一个对象比较方法:)
For simple objects you could use JSON.stringify(obj1) === JSON.stringify(obj2)
. 对于简单对象,可以使用JSON.stringify(obj1) === JSON.stringify(obj2)
。
More info on object comparison can be found in this answer 关于对象比较的更多信息可以在此答案中找到
With an invention of Object.prototype.compare()
and Array.prototype.compare()
this job becomes a very simple task. 通过Object.prototype.compare()
和Array.prototype.compare()
的发明,这项工作变得非常简单。 Array compare can handle both primitive and reference type items. 数组比较可以处理原始类型和引用类型的项目。 Objects are compared shallow. 对象比较浅。 Let's see how it works; 让我们看看它是如何工作的。
Object.prototype.compare = function(o){ var ok = Object.keys(this); return typeof o === "object" && ok.length === Object.keys(o).length ? ok.every(k => this[k] === o[k]) : false; }; Array.prototype.compare = function(a){ return this.every((e,i) => typeof a[i] === "object" ? a[i].compare(e) : a[i] === e); }; var fruits1 = [{"apple":25},{"bannana":36},{"orange":6}], fruits2 = [{"apple":25},{"bannana":36},{"orange":6}]; console.log(fruits1.compare(fruits2));
Use underscore array functions. 使用下划线数组函数。 I would go with intersection 我会去十字路口
http://underscorejs.org/#intersection http://underscorejs.org/#intersection
You can use the following static
method for Object
class : Object.equals 您可以对Object
类使用以下static
方法: Object.equals
Object.equals=function(a,b){if(a===b)return!0;if(!(a instanceof Object&&b instanceof Object))return!1;if(a.valueOf()===b.valueOf())return!0;if(a.constructor!==b.constructor)return!1;for(var c in a)if(a.hasOwnProperty(c)){if(!b.hasOwnProperty(c))return!1;if(a[c]!==b[c]){if("object"!=typeof a[c])return!1;if(!Object.equals(a[c],b[c]))return!1}}for(c in b)if(b.hasOwnProperty(c)&&!a.hasOwnProperty(c))return!1;return!0}; console.log( `"[1,2,3] == [1,2,3]" ?`,Object.equals([1,2,3],[1,2,3]) ); console.log( `"[{"apple":25},{"bannana":36},{"orange":6}] == [{"apple":25},{"bannana":36},{"orange":6}]" ?`,Object.equals([{"apple":25},{"bannana":36},{"orange":6}], [{"apple":25},{"bannana":36},{"orange":6}]) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.