我有以下简单数组
['a', 'b', 'a', 'c', 'a', 'c', 'd', 'a']
Ramda 如何帮助我实现以下目标
{a: 4, b: 1, c: 2, d: 1}
a:4
表示值 a
在主数组中存在 4
次
b:1
表示值 b
在主数组中存在 1
次
c:2
表示值 c
在主数组中存在 2
次
d:1
表示值 d
在主数组中存在 1
次
最佳答案
使用R.countBy
与 R.identity
作为生成 key 的函数:
const data = ['a', 'b', 'a', 'c', 'a', 'c', 'd', 'a']
const result = R.countBy(R.identity, data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
关于javascript - Ramda,计数数组中的值频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59180745/