Javascript 如何比较节点js中的两个数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13523611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two arrays in node js?
提问by gauti
I am having two arrays, how can i compare the two arrays at single shot.
我有两个阵列,我如何一次比较这两个阵列。
var arr1= ["a","b","c"];
var arr2 = ["a","c","d"]
if(arr1 == arr2){
console.log(true);
}else{
console.log(false);
}
回答by Nathan Wall
var arr1 = ["a","b","c"];
var arr2 = ["a","c","d"];
if (arr1.length == arr2.length
&& arr1.every(function(u, i) {
return u === arr2[i];
})
) {
console.log(true);
} else {
console.log(false);
}
Side note for edge cases:
边缘情况的旁注:
===is often considered slightly broken for this kind of task because NaNbehaves unexpectedly:
===对于此类任务,通常被认为是轻微损坏,因为NaN表现出乎意料:
var arr1 = ["a",NaN,"b"];
var arr2 = ["a",NaN,"b"];
if (arr1.length == arr2.length
&& arr1.every(function(u, i) {
return u === arr2[i];
})
) {
console.log(true);
} else {
console.log(false);
}
The code above actually logs falsebecause NaN !== NaN. In addition, ===can't distinguish +0from -0. To cover both of these cases, you could use a stronger comparison known as "egal" or "is", which can easily be implemented like so:
上面的代码实际上记录了false因为NaN !== NaN. 此外,===不能区分+0从-0。为了涵盖这两种情况,您可以使用称为“egal”或“is”的更强比较,它可以像这样轻松实现:
function is(a, b) {
return a === b && (a !== 0 || 1 / a === 1 / b) // false for +0 vs -0
|| a !== a && b !== b; // true for NaN vs NaN
}
var arr1 = ["a",NaN,"b"];
var arr2 = ["a",NaN,"b"];
if (arr1.length == arr2.length
&& arr1.every(function(u, i) {
// Use "is" instead of "==="
return is(u, arr2[i]);
})
) {
console.log(true);
} else {
console.log(false);
}
回答by staackuser2
回答by Taihwan Hah
[ES6]
[ES6]
Top answer is good & enough.
最佳答案是好的和足够的。
But when you just want to compare its values are same you have to sort it before. here's no need sort code.
但是当您只想比较其值是否相同时,您必须先对其进行排序。这里不需要排序代码。
if(arr1.length == arr2.length && arr1.every((v) => arr2.indexOf(v) >= 0)) {
console.log(true);
} else {
console.log(false);
}
And.. I think using a 'some' instead of 'every' is better.
而且..我认为使用“一些”而不是“每个”更好。
If those are not same, 'some' gives you a early exit. - very little early but early ;)
如果这些不一样,“一些”会让你提前退出。- 很少早但早;)
if(arr1.length == arr2.length && !arr1.some((v) => arr2.indexOf(v) < 0)) {
console.log(true);
} else {
console.log(false);
}
回答by incarnate
Here's another one, without ES5 every:
这是另一个,没有 ES5 every:
function arrEq(arr1, arr2) {
for (var i = 0; i < arr1.length; i++)
if (arr1[i] != arr2[i])
return false;
return i == arr2.length;
}
回答by LeRoss
I wanted to add some modification of the code made by 'Taihwan Hah' but could not leave a comment (the system told me so)
我想对'Taihwan Hah'所做的代码进行一些修改,但无法发表评论(系统告诉我)
So here is my modifs:
所以这是我的修改:
function ArrayEquals(arr1,arr2){
return arr1.length === arr2.length && !arr1.some((v) => arr2.indexOf(v) < 0) && !arr2.some((v) => arr1.indexOf(v) < 0);
}
basically, I had to check for but array because my arrays do not contains unique numbers.
基本上,我不得不检查但数组,因为我的数组不包含唯一数字。
回答by Robin Cai
I would like to improve the answer from staackuser2 a little bit:
我想稍微改进 staackuser2 的答案:
var same = (arr1.length === arr2.length) && (_.difference(arr1, arr2).length === 0)
or
或者
var same = (_.difference(arr1, arr2).length === 0) && (_.difference(arr2, arr1).length === 0)
回答by ibodi
The top answer is good, but I would also consider using Array.prototype:
最佳答案是好的,但我也会考虑使用 Array.prototype:
Array.prototype.equals = function (arr) {
return this.length == arr.length && this.every((u, i) => u === arr[i]);
}
console.log([1,2,3].equals([1,2,3])); // true
console.log([1,2,3].equals([1,3,3])); // false
// BUT!
console.log(["a",NaN,"b"].equals(["a",NaN,"b"])); // false, because NaN !== NaN
If you want it to work for NaNs too and distinguish +0 and -0, better use this:
如果您希望它也适用于 NaN 并区分 +0 和 -0,最好使用以下命令:
Array.prototype.equals = function (arr) {
function is(a, b) { // taken from the top answer
return a === b && (a !== 0 || 1 / a === 1 / b) // false for +0 vs -0
|| a !== a && b !== b; // true for NaN vs NaN
}
return this.length == arr.length && this.every((u, i) => is(u, arr[i]));
}
console.log(["a",NaN,"b"].equals(["a",NaN,"b"])); // true

