是否有一种简洁的方法来获得与以下相同的结果,但是:
我曾考虑过使用reduce更新包含传递/失败数组的累加器对象,但是我不确定是否有一个更聪明/更冗长的选项
const predicate = i => i % 2 === 0;
const predicatePassed = [];
const predicateFailed = [];
[1, 2, 3, 4].forEach(i => predicate(i) ? predicatePassed.push(i) : predicateFailed.push(i))
// predicatePassed === [2, 4]
// predicateFailed === [1, 3]
答案 0 :(得分:4)
您可以使用R.partition:
const predicate = i => i % 2 === 0;
const [predicatePassed, predicateFailed] = R.partition(predicate)([1, 2, 3, 4]);
console.log(predicatePassed);
console.log(predicateFailed);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>