最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ramda, array equality regardless of order - Stack Overflow

programmeradmin1浏览0评论

When paring arrays, the ramda equals will return true only if two arrays hold the same values in the same order.

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?

When paring arrays, the ramda equals will return true only if two arrays hold the same values in the same order.

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?

Share Improve this question asked Jun 10, 2018 at 11:48 devboelldevboell 1,1902 gold badges16 silver badges34 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 9

I think your answer is fine. A slightly shorter one would be

const equalIgnoreOrder = pose(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. But that's a pretty minor concern here.

发布评论

评论列表(0)

  1. 暂无评论