[英]How to get two arrays by mapping a predicate over an input array with Ramda
Is there a concise way to achieve the same result as the below but: 有没有一种简洁的方法来实现与以下相同的结果,但是:
I thought about using reduce to update an accumulator object containing the passed/failed arrays, but I'm not sure if there was a smarter/less verbose option 我考虑过使用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]
You can use R.partition : 您可以使用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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.