[英]How to compare two arrays in node js?
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);
}
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 NaN
behaves 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 false
because NaN !== NaN
. 上面的代码实际上记录为
false
因为NaN !== NaN
。 In addition, ===
can't distinguish +0
from -0
. 另外,
===
不能区分+0
和-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: 为了涵盖这两种情况,您可以使用更强的比较,称为“ 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);
}
我将为此使用下划线 。
var same = (_.difference(arr1, arr2).length == 0)
[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);
}
Here's another one, without ES5 every
: 这是另一个,
every
没有ES5:
function arrEq(arr1, arr2) {
for (var i = 0; i < arr1.length; i++)
if (arr1[i] != arr2[i])
return false;
return i == arr2.length;
}
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. 基本上,我必须检查but数组,因为我的数组不包含唯一数字。
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)
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
(Although the question is much older than v9.0.0, but) since the question is about Node - starting from Node v9.0.0 you can use the built-in "util" module's isDeepStrictEqual(arr1, arr2) for comparing arrays (or objects, or anything for that matter) (虽然这个问题比 v9.0.0 早得多,但是)因为问题是关于 Node - 从 Node v9.0.0 开始,你可以使用内置的“util”模块的isDeepStrictEqual(arr1, arr2)来比较数组(或对象) ,或任何与此有关的东西)
const util = require('util');
let arr1 = [1,2,3];
let arr2 = [1,2,3];
let arr3 = [1,3,2];
console.log(util.isDeepStrictEqual(arr1, arr2)) // true
console.log(util.isDeepStrictEqual(arr1, arr3)) // false
Also works for NaN也适用于 NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.