Skip to content

Instantly share code, notes, and snippets.

@queviva
Last active December 7, 2023 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save queviva/43cf075996da3a07b85c949e9f64e1eb to your computer and use it in GitHub Desktop.
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
const sortGoodBad = (r, f = x => Number(x)) => r.reduce(
(a, v) => (a[(f(v)) ? 0 : 1].push(v), a), [[], []]
);
@queviva
Copy link
Author

queviva commented Aug 3, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment