Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
73
rated 0 times [  77] [ 4]  / answers: 1 / hits: 119667  / 14 Years ago, tue, october 26, 2010, 12:00:00

I want to check if the two arrays are identical
(not content wise, but in exact order).



For example:



 array1 = [1,2,3,4,5]
array2 = [1,2,3,4,5]
array3 = [3,5,1,2,4]


Array 1 and 2 are identical but 3 is not.



Is there a good way to do this in JavaScript?


More From » arrays

 Answers
15

So, what's wrong with checking each element iteratively?



function arraysEqual(arr1, arr2) {
if(arr1.length !== arr2.length)
return false;
for(var i = arr1.length; i--;) {
if(arr1[i] !== arr2[i])
return false;
}

return true;
}

[#95170] Saturday, October 23, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tyreese

Total Points: 739
Total Questions: 95
Total Answers: 98

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
;