js compare two array code example
Example 1: javascript Compare two arrays regardless of order
const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
isEqual([1, 2, 3], [1, 2, 3]);
isEqual([1, 2, 3], [1, '2', 3]);
Example 2: how to compare two arrays javascript
function arraysAreIdentical(arr1, arr2){
if (arr1.length !== arr2.length) return false;
for (var i = 0, len = arr1.length; i < len; i++){
if (arr1[i] !== arr2[i]){
return false;
}
}
return true;
}
Example 3: compare arrays javascript
const array1 = [1,2,3,4]
const array2 = [1,2,3,4]
array1.join() === array2.join()
const array3 = [1,2,3,[1,2,3]]
const array4 = [1,2,3,[1,2,3]]
JSON.stringify(array3) === JSON.stringify(array4)