[英]Ramda, array equality regardless of order
When comparing arrays, the ramda equals
will return true only if two arrays hold the same values in the same order. 比较数组时,仅当两个数组以相同顺序持有相同值时,ramda equals
才会返回true。
I need a function to check if two arrays hold exactly the same values, but ignore the order in which the values occur. 我需要一个函数来检查两个数组是否持有完全相同的值,但是忽略这些值出现的顺序。
For now I am doing it this way: 现在,我这样做:
const equalLength = (arr1, arr2) => arr1.length === arr2.length
export const equalIgnoreOrder = (arr1, arr2) =>
equalLength(arr1, arr2) && equalLength(arr1, R.union(arr1, arr2))
but I am wondering if there is a more 'out of the box' solution? 但我想知道是否还有更多“开箱即用”的解决方案?
I think your answer is fine. 我认为您的回答很好。 A slightly shorter one would be 稍微短一点的是
const equalIgnoreOrder = compose(isEmpty, symmetricDifference)
This feels a bit more logical to me, as checking for the same elements feels more like a question of differences than unions; 对我来说,这有点合乎逻辑,因为检查相同的元素比结合起来更像是一个差异问题。 it feels closer to the mathematical idea of sets than does one that involves length
. 与涉及length
的数学概念相比,它更接近数学的集合概念。 But that's a pretty minor concern here. 但这是一个很小的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.