Splits values into two groups, based on the result of the given filtering function.
Array.prototype.reduce()
and Array.prototype.push()
to add elements to groups, based on the value returned by fn
for each element.fn
returns a truthy value for any element, add it to the first group, otherwise add it to the second group.const bifurcateBy = (arr, fn) =>
arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [
[],
[],
]);
bifurcateBy(['beep', 'boop', 'foo', 'bar'], x => x[0] === 'b');
// [ ['beep', 'boop', 'bar'], ['foo'] ]
JavaScript, Array
Splits values into two groups, based on the result of the given filter
array.
JavaScript, Array
Mutates the original array to filter out the values specified, based on a given iterator function.
JavaScript, Array
Groups the elements of an array based on the given function and returns the count of elements in each group.