The reason it doesn't work like that -- beyond the fact that the Ramda function is named equals
and not isEqual
-- is that Arrays are intrinsically ordered containers. [1, 2]
is materially different from [2, 1]
.
The standard unordered container is the Set
. Unfortunately that is based on reference equality, so it could get multiple copies of items Ramda would think of as equal. So the most obvious answer will not work properly:
// ** Broken -- do not use **
const eqValues = (a1, a2) => R.equals(new Set(a1), new Set(a2))
console.log(eqValues(
[{x: 1}, {x: 2}],
[{x: 1}, {x: 3}]
)) //=> false
console.log(eqValues(
[{x: 1}, {x: 2}],
[{x: 2}, {x: 1}]
)) //=> true
because it would fail due to a length check in this case:
console.log(eqValues(
[{x: 1}, {x: 2}, {x: 2}],
[{x: 2}, {x: 1}]
)) //=> false, but should be true, since {x: 2} is the same as {x: 2}
Ramda does not expose its internal _Set
type -- and perhaps it should -- but it uses them in such functions as difference
, and through that in symmetricDifference
. These are appropriate functions for testing values whose value equality is in question.
So my answer would be similar to the one from bugs, but I would phrase it a bit differently:
const eqValues = compose(isEmpty, symmetricDifference)
console.log(eqValues(
[{x: 1}, {x: 2}],
[{x: 1}, {x: 3}]
)) //=> false
console.log(eqValues(
[{x: 1}, {x: 2}],
[{x: 2}, {x: 1}]
)) //=> true
console.log(eqValues(
[{x: 1}, {x: 2}],
[{x: 2}, {x: 1}, {x: 1}]
)) //=> true
<script src="https://bundle.run/ramda@0.26.1"></script><script>
const {compose, isEmpty, symmetricDifference} = ramda; </script>
However, if you need to test multiplicities -- that is, arr1
contains two copies of {x: 42}
and arr2
only has one, so they're different -- then I would use the answer from customcommander.