【发布时间】:2021-06-25 21:10:49
【问题描述】:
我已经使用 Ramda 制作了一个解析功能,它就像一个魅力,但我很确定它可以被简化,不幸的是我正在努力克服这个优化
我想将这些值分组到一个具有“名称”且“y”是计数的结构中。例如,如果“待定”出现两次,并且在我的输入中出现“雇用”一次,那么我想得到你在输出中看到的内容
我写的函数
const aggResumeStatusTest = data =>
reduce(
(acc, elem) => {
if (!isEmpty(acc)) {
const indexInArray = findIndex(propEq('name', elem))(acc);
if (indexInArray !== -1) {
acc[indexInArray].y++;
} else {
acc.push({
name: elem,
y: 1,
});
}
return acc;
}
acc.push({
name: elem,
y: 1,
});
return acc;
},
[],
data,
);
aggResumeStatusTest(['pending', 'pending', 'hired'])
输入
['pending', 'pending', 'hired']
输出
[ { name: 'pending', y: 2 }, { name: 'hired', y: 1 } ]
提前感谢您的帮助
【问题讨论】:
标签: javascript ramda.js