Last active
December 7, 2023 22:35
-
-
Save queviva/43cf075996da3a07b85c949e9f64e1eb to your computer and use it in GitHub Desktop.
splits up an array into two arrays, one that matches and one that does not match
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sortGoodBad = (r, f = x => Number(x)) => r.reduce( | |
(a, v) => (a[(f(v)) ? 0 : 1].push(v), a), [[], []] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this will return a new array consisting of two arrays; the first contains elements that match a given function and the second contains elements that do not match; the original array [orray, as his friends call him] remains unmutated
call by passing an array and an optional comparison function - this would split an array into values which are less than a hundred and elements which are not:
sortGoodBad(originalArray, v => v < 100);
by default, sortGoodBad will separate things which are, or could possibly be, numbers from things that are certainly not numbers