700 B
700 B
title, type, tags, cover, dateModified
| title | type | tags | cover | dateModified | |
|---|---|---|---|---|---|
| Bifurcate array based on values | snippet |
|
two-cities | 2020-11-01T20:50:57+02:00 |
Splits values into two groups, based on the result of the given filter array.
- Use
Array.prototype.reduce()andArray.prototype.push()to add elements to groups, based onfilter. - If
filterhas a truthy value for any element, add it to the first group, otherwise add it to the second group.
const bifurcate = (arr, filter) =>
arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [
[],
[],
]);
bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]);
// [ ['beep', 'boop', 'bar'], ['foo'] ]