javascript compare array values code example
Example 1: javascript compare two arrays of objects get same elements
var result = result1.filter(function (o1) {
return result2.some(function (o2) {
return o1.id === o2.id;
});
});
let result = result1.filter(o1 => result2.some(o2 => o1.id === o2.id));
Example 2: javascript compare arrays
Array.prototype.equals = function(arr2) {
return (
this.length === arr2.length &&
this.every((value, index) => value === arr2[index])
);
};
[1, 2, 3].equals([1, 2, 3]);
[1, 2, 3].equals([3, 6, 4, 2]);
Example 3: 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);