我有一个具有重复值的数组,我需要使用ramda.js查找每个值在数组中发生的次数。
这是我的阵列: 2013,2013,2013,2014,2014,2014,2014,2015,2015,2015,2015,2015,2015,2015,2015,2016,2016,2016,2016,2016,2016,2017,2017,2017,2017
这就是我想要得到的: 3,4,7,5,3
下面是一个示例,说明它如何在纯JavaScript中工作。
function count (arr) {
const counts = {}
arr.forEach((x) => { counts[x] = (counts[x] || 0) + 1 })
return Object.values(counts)
}
发布于 2018-12-10 18:27:20
假设(就像在您的代码中)不需要按顺序排列,您可以使用R.countBy()
和R.values()
获得相同的结果
const { pipe, countBy, identity, values } = R;
const arr = [2013, 2013, 2013, 2014, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2016, 2017, 2017, 2017]
const countDupes = pipe(
countBy(identity),
values
)
console.log(countDupes(arr));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
https://stackoverflow.com/questions/53711491
复制相似问题