我有这两个数组: 一个数组填充来自 ajax 请求的信息,另一个数组存储用户单击的按钮。我使用这个代码(我用样本号码填充) :
var array1 = [2, 4];
var array2 = [4, 2]; //It cames from the user button clicks, so it might be disordered.
array1.sort(); //Sorts both Ajax and user info.
array2.sort();
if (array1==array2) {
doSomething();
}else{
doAnotherThing();
}
但它总是给出 false
,即使这两个数组相同,但名称不同。(我在 Chrome 的 JS 控制台中检查了这一点)。那么,有没有办法知道这两个数组是否包含相同的内容呢?为什么是 false
?如何知道第一个数组中的哪些值不在第二个数组中?