我有这两个数组:一个填充来自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();
}
Run Code Online (Sandbox Code Playgroud)
但它始终给出false,即使两个数组相同,但名称不同.(我在Chrome的JS控制台中查看了这个).那么,有什么方法可以知道这两个数组是否包含相同的数据?它为什么要给予false?我如何知道第一个数组中的哪些值不在第二个数组中?