This question already has an answer here:
這個問題已經有了答案:
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.
數組1和2是相同的,但是3不是。
Is there a good way to do this in JavaScript?
在JavaScript中是否有一種很好的方法來實現這一點?
104
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;
}
28
You could compare String representations so:
你可以比較字符串表示形式:
array1.toString() == array2.toString()
array1.toString() !== array3.toString()
but that would also make
但這也會產生
array4 = ['1',2,3,4,5]
equal to array1 if that matters to you
等於array1
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/10/26/df6221ea489f7deb88265bd5d140d0a3.html。