🚬 Extract items from one array into two or more arrays by results of a provided function
const arraySelect = ; const odd even = ;
Arguments
const results = arraySelect(Array, Function[, ...Functions]);
Functions accept three parameters (just like all array iterator functions):
element {Any}
: The current element being processed in the arrayindex {Number}
: The index of the current element being processed in the array.array {Array}
The array being processed
The function should return a truthy or falsy value (coerced into a boolean for testing purposes)
Passing one function will create two arrays
const odd even = ; odd // [1, 3, 5, 7, 9]even // [2, 4, 6, 8, 10]
Passing more than one function will result in same number of arrays. Elements can be included in more than one result array
const adult male = ; adult // [{gender: 'male', age: 64}, {gender: 'male', age: 21}, {gender: 'female', age: 32}]male // [{gender: 'male', age: 8}, {gender: 'male', age: 64}, {gender: 'male', age: 21}]
A callback function that uses more arguments
const original duplicates = ; original // [1, 2, 3, 4, 8]duplicates // [1, 1, 3]
Transpiled version
Environments which exclude node_modules from the transpiling pipeline should include the "browser" entry instead of "main". This exposes an ES5 commonjs module.
Also available for explicit import:
const arraySelect = ;