如何检查两个数组是否与JavaScript相等?var a = [1, 2, 3];var b = [3, 2, 1];var c = new Array(1, 2, 3);alert(a == b + "|" + b == c);演示如何检查这些数组是否相等,以及如何获得返回true如果他们是平等的?jQuery提供了这样的方法吗?
3 回答
函数式编程
TA贡献1807条经验 获得超9个赞
stringify
< >
.
function arraysEqual(a, b) { if (a === b) return true; if (a == null || b == null) return false; if (a.length != b.length) return false; // If you don't care about the order of the elements inside // the array, you should sort both arrays here. // Please note that calling sort on an array will modify that array. // you might want to clone your array first. for (var i = 0; i < a.length; ++i) { if (a[i] !== b[i]) return false; } return true;}
MYYA
TA贡献1868条经验 获得超4个赞
var a=[1,2,3];var b=[3,2,1];var c=new Array(1,2,3);alert(_.isEqual(a, b) + "|" + _.isEqual(b, c));
编辑:
_.isEqual([1,2,3], [1,2,3]); // true_.isEqual([1,2,3], [3,2,1]); // false
sort
:
_.isEqual([1,2,3].sort(), [3,2,1].sort()); // true
添加回答
举报
0/150
提交
取消