var a = [1, 2, 3];
var b = [3, 2, 1];
var c = new Array(1, 2, 3);
alert(a == b + "|" + b == c);
demo
如何检查这些数组是否相等,并获得一个方法,如果它们相等,则返回true ?
jQuery是否为此提供了任何方法?
var a = [1, 2, 3];
var b = [3, 2, 1];
var c = new Array(1, 2, 3);
alert(a == b + "|" + b == c);
demo
如何检查这些数组是否相等,并获得一个方法,如果它们相等,则返回true ?
jQuery是否为此提供了任何方法?
当前回答
即使这看起来超级简单,但有时它真的很有用。如果你所需要的只是看看两个数组是否有相同的项,并且它们的顺序相同,试试这个:
[1, 2, 3].toString() == [1, 2, 3].toString()
true
[1, 2, 3,].toString() == [1, 2, 3].toString()
true
[1,2,3].toString() == [1, 2, 3].toString()
true
然而,这并不适用于模式高级情况,如:
[[1,2],[3]].toString() == [[1],[2,3]].toString()
true
这取决于你需要什么。
其他回答
即使这看起来超级简单,但有时它真的很有用。如果你所需要的只是看看两个数组是否有相同的项,并且它们的顺序相同,试试这个:
[1, 2, 3].toString() == [1, 2, 3].toString()
true
[1, 2, 3,].toString() == [1, 2, 3].toString()
true
[1,2,3].toString() == [1, 2, 3].toString()
true
然而,这并不适用于模式高级情况,如:
[[1,2],[3]].toString() == [[1],[2,3]].toString()
true
这取决于你需要什么。
对于数字和字符串等原始值,这是一个简单的解决方案:
a = [1,2,3]
b = [3,2,1]
a.sort().toString() == b.sort().toString()
调用sort()将确保元素的顺序无关紧要。toString()调用将创建一个值以逗号分隔的字符串,以便可以测试两个字符串是否相等。
如果您希望检查对象数组的相等性和顺序并不重要,即。
areEqual ([{id:“0”},{id: " 1 "}], [{id: " 1 "}, {id: " 0 "})) / /真实的
首先要对数组进行排序。lodash有你需要的所有工具,通过组合sortBy和isEqual:
// arr1 & arr2: Arrays of objects
// sortProperty: the property of the object with which you want to sort
// Note: ensure every object in both arrays has your chosen sortProperty
// For example, arr1 = [{id: "v-test_id0"}, {id: "v-test_id1"}]
// and arr2 = [{id: "v-test_id1"}, {id: "v-test_id0"}]
// sortProperty should be 'id'
function areEqual (arr1, arr2, sortProperty) {
return _.areEqual(_.sortBy(arr1, sortProperty), _.sortBy(arr2, sortProperty))
}
编辑:由于sortBy返回一个新数组,在排序之前不需要克隆数组。原始数组不会被改变。
注意,对于lodash的isEqual,顺序很重要。如果sortBy没有首先应用于每个数组,上面的例子将返回false。
它处理所有可能的东西,甚至在对象的结构中引用自己。您可以在代码的末尾看到示例。
var deepCompare = (function() {
function internalDeepCompare (obj1, obj2, objects) {
var i, objPair;
if (obj1 === obj2) {
return true;
}
i = objects.length;
while (i--) {
objPair = objects[i];
if ( (objPair.obj1 === obj1 && objPair.obj2 === obj2) ||
(objPair.obj1 === obj2 && objPair.obj2 === obj1) ) {
return true;
}
}
objects.push({obj1: obj1, obj2: obj2});
if (obj1 instanceof Array) {
if (!(obj2 instanceof Array)) {
return false;
}
i = obj1.length;
if (i !== obj2.length) {
return false;
}
while (i--) {
if (!internalDeepCompare(obj1[i], obj2[i], objects)) {
return false;
}
}
}
else {
switch (typeof obj1) {
case "object":
// deal with null
if (!(obj2 && obj1.constructor === obj2.constructor)) {
return false;
}
if (obj1 instanceof RegExp) {
if (!(obj2 instanceof RegExp && obj1.source === obj2.source)) {
return false;
}
}
else if (obj1 instanceof Date) {
if (!(obj2 instanceof Date && obj1.getTime() === obj2.getTime())) {
return false;
}
}
else {
for (i in obj1) {
if (obj1.hasOwnProperty(i)) {
if (!(obj2.hasOwnProperty(i) && internalDeepCompare(obj1[i], obj2[i], objects))) {
return false;
}
}
}
}
break;
case "function":
if (!(typeof obj2 === "function" && obj1+"" === obj2+"")) {
return false;
}
break;
default: //deal with NaN
if (obj1 !== obj2 && obj1 === obj1 && obj2 === obj2) {
return false;
}
}
}
return true;
}
return function (obj1, obj2) {
return internalDeepCompare(obj1, obj2, []);
};
}());
/*
var a = [a, undefined, new Date(10), /.+/, {a:2}, function(){}, Infinity, -Infinity, NaN, 0, -0, 1, [4,5], "1", "-1", "a", null],
b = [b, undefined, new Date(10), /.+/, {a:2}, function(){}, Infinity, -Infinity, NaN, 0, -0, 1, [4,5], "1", "-1", "a", null];
deepCompare(a, b);
*/
jQuery没有比较数组的方法。然而,下划线库(或类似的Lodash库)确实有这样一个方法:isEqual,它也可以处理各种其他情况(如对象字面量)。坚持提供的例子:
var a=[1,2,3];
var b=[3,2,1];
var c=new Array(1,2,3);
alert(_.isEqual(a, b) + "|" + _.isEqual(b, c));
顺便说一句:下划线还有很多jQuery没有的方法,所以它是jQuery的一个很好的补充。
编辑:正如评论中所指出的,现在只有当两个数组的元素顺序相同时,上述方法才有效,即:
_.isEqual([1,2,3], [1,2,3]); // true
_.isEqual([1,2,3], [3,2,1]); // false
幸运的是,Javascript有一个内置的方法来解决这个问题,sort:
_.isEqual([1,2,3].sort(), [3,2,1].sort()); // true