Categories
Splits an array into two arrays based on a callback function.
Contributed by @itsbrunodev
function partition(arr, callback) { return arr.reduce( ([pass, fail], elem) => { if (callback(elem)) { pass.push(elem); } else { fail.push(elem); } return [pass, fail]; }, [[], []] ); }
const arr = [1, 2, 3, 4, 5]; const isEven = (n) => n % 2 === 0; partition(arr, isEven); // [[2, 4], [1, 3, 5]]