我试图检查两个数组是否相同,但不适用于长度为1的数组!
控制台输出预期为
true
false
true
false
var test1 = [0,1,2];
var test2 = [0,1,2];
console.log(test1.toString() == test2.toString());
console.log(test1.toString() == !test2.toString());
test1 = [0];
test2 = [0];
console.log(test1.toString() == test2.toString());
console.log(test1.toString() == !test2.toString());
答案 0 :(得分:5)
因为最后一个被评估为:
[0].toString() == ![0].toString()
"0" == !"0"
"0" == !true
"0" == false
0 == false
false == false
true
TLDR:不要试图通过对它们进行字符串化来比较它们,这总是会导致一些意想不到的副作用。
答案 1 :(得分:2)
要测试某项是否“不相等”,请使用x != y
或x !== y
而不是x == !y
:
var test1 = [0,1,2];
var test2 = [0,1,2];
console.log(test1.toString() == test2.toString());
console.log(test1.toString() != test2.toString());
test1 = [0];
test2 = [0];
console.log(test1.toString() == test2.toString());
console.log(test1.toString() != test2.toString());
// third test to make sure we see that different arrays are different
test1 = [0];
test2 = [0, 1];
console.log(test1.toString() == test2.toString());
console.log(test1.toString() != test2.toString());
// another kind of different
test1 = [0];
test2 = [1];
console.log(test1.toString() == test2.toString());
console.log(test1.toString() != test2.toString());
但是,将数组转换为字符串以确定它们是否等效,尽管它可以处理数字数组,但通常情况下不起作用。您需要比较长度,如果长度相同,则可以使用every
来检查元素。像这样:
function arrayEqual(a, b) {
return a.length === b.length &&
a.every((v, i) => v == b[i]);
}
var test1 = [0,1,2];
var test2 = [0,1,2];
console.log(arrayEqual(test1, test2));
console.log(!arrayEqual(test1, test2));
test1 = [0];
test2 = [0];
console.log(arrayEqual(test1, test2));
console.log(!arrayEqual(test1, test2));
// third test to make sure we see that different arrays are different
test1 = [0];
test2 = [0, 1];
console.log(arrayEqual(test1, test2));
console.log(!arrayEqual(test1, test2));
// another kind of different
test1 = [0];
test2 = [1];
console.log(arrayEqual(test1, test2));
console.log(!arrayEqual(test1, test2));
答案 2 :(得分:0)
您可以使用严格比较(===
和!==
)来工作,并跳过!
布尔否定运算符的使用,该运算符会将字符串强制为布尔值。您可以查看coercion以获得更多信息。
var test1 = [0,1,2];
var test2 = [0,1,2];
console.log(test1.toString() === test2.toString());
console.log(test1.toString() !== test2.toString());
test1 = [0];
test2 = [0];
console.log(test1.toString() === test2.toString());
console.log(test1.toString() !== test2.toString());
但是,这种方法不适用于所有情况,例如:
var test1 = [""];
var test2 = [null];
console.log(test1.toString() === test2.toString());
console.log(test1.toString() !== test2.toString());
最好使用类似Array.every()方法的东西:
var test1 = [""];
var test2 = [null];
var test3 = [1,2,3,4];
var test4 = [1,2,3,4];
const compareArray = (a1, a2) =>
{
return a1.length === a2.length && a1.every((val, idx) => val === a2[idx]);
}
console.log(compareArray(test1, test2));
console.log(compareArray(test3, test4));
答案 3 :(得分:0)
您可以尝试这种方法吗?
var test1 = [0,1,2];
var test2 = [0,1,2];
console.log(test1.every((item, i) => test2[i] === item));
console.log(!test1.every((item, i) => test2[i] === item));
test1 = [0];
test2 = [0];
console.log(test1.every((item, i) => test2[i] === item));
console.log(!test1.every((item, i) => test2[i] === item));
我确定它会起作用。 希望对您有帮助,祝您有美好的一天。