how can we compare arrays in js code example
Example 1: best way compare arrays javascript
const array1 = ['potato', 'banana', 'soup']
const array2 = ['potato', 'orange', 'soup']
array1 === array2;
JSON.stringify(array1) === JSON.stringify(array2);
const deepArray1 = [{test: 'dummy'}, [['woo', 'ya'], 'weird']]
const deepArray2 = [{test: 'dummy'}, [['woo', 'ya'], 'weird']]
deepArray1 === deepArray2;
JSON.stringify(deepArray1) === JSON.stringify(deepArray2);
Example 2: comparing two arrays in javascript
const arr1 = [1, 2, 3];
const arr2 = [1, 3, 3];
if (arr1.length !== arr2.length) return console.log("false");
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if (arr1[i] === arr2[j]) {
console.log("yes match", arr1[i], arr2[j]);
continue;
}
console.log("no match", arr1[i], arr2[j]);
}
}