how to compare inside array elements in javascript code example
Example 1: best way compare arrays javascript
// To compare arrays (or any other object):
// Simple Array Example:
const array1 = ['potato', 'banana', 'soup']
const array2 = ['potato', 'orange', 'soup']
array1 === array2;
// Returns false due to referential equality
JSON.stringify(array1) === JSON.stringify(array2);
// Returns true
// Another Example:
const deepArray1 = [{test: 'dummy'}, [['woo', 'ya'], 'weird']]
const deepArray2 = [{test: 'dummy'}, [['woo', 'ya'], 'weird']]
deepArray1 === deepArray2;
// Returns false due to referential equality
JSON.stringify(deepArray1) === JSON.stringify(deepArray2);
// Returns true
Example 2: how to compare elements in an array
for (let i = 0; i < a.length; i++) {
for (let k = i + 1; k < a.length; k++) {
if (a[i] != a[k]) {
//do stuff
}
}
}