I have to compare two unsorted arrays of objects, for example the following code should return true:
compareObjs(
[{ foo: 'foo', bar: 'bar' }, { baz: 'baz' }],
[{ baz: 'baz' }, { foo: 'foo', bar: 'bar' }]
)
I know there are already plenty of answers about comparing arrays of objects, but I did not really find the clear one for comparing the unsorted version of arrays.
Here another possibility:
const array2 = [1,3,2,4,5];
const isInArray1 = array1.every(item => array2.find(item2 => item===item2))
const isInArray2 = array2.every(item => array1.find(item2 => item===item2))
const isSameArray = array1.length === array2.length && isInArray1 && isInArray2
console.log(isSameArray); //true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With