Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

javascript - 如何用JavaScript检查两个数组是否相等? [重复](How to check if two arrays are equal with JavaScript? [duplicate])

This question already has an answer here:(这个问题已经在这里有了答案:)

How to compare arrays in JavaScript?(如何在JavaScript中比较数组?) 55 answers(55个答案)
var a = [1, 2, 3];
var b = [3, 2, 1];
var c = new Array(1, 2, 3);

alert(a == b + "|" + b == c);

demo(演示)

How can I check these array for equality and get a method which returns true if they are equal?(我如何检查这些数组是否相等并获得一个如果它们相等则返回true的方法?) Does jQuery offer any method for this?(jQuery是否为此提供任何方法?)   ask by Koerr translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is what you should do.(这是您应该做的。)

Please don't use stringify nor < > .(请不要使用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; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...