Reputation: 65
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
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]
Upvotes: 2
Views: 286
Reputation: 191986
You can use 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>
Upvotes: 7