Skip to content

Instantly share code, notes, and snippets.

@roshanca
Created July 12, 2019 04:07
Show Gist options
  • Save roshanca/8e2f2cdf2648eee8a14d91b44818db47 to your computer and use it in GitHub Desktop.
Save roshanca/8e2f2cdf2648eee8a14d91b44818db47 to your computer and use it in GitHub Desktop.
One loop two arrays #js
const exampleValues = [2, 15, 8, 23, 1, 32];
const [truthyValues, falseyValues] = exampleValues.reduce((arrays, exampleValue) => {
if (exampleValue > 10) {
arrays[0].push(exampleValue);
return arrays;
}
arrays[1].push(exampleValue);
return arrays;
}, [[], []]);
// truthyValues
// (3) [15, 23, 32]
// falseyValues
// (3) [2, 8, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment