Skip to content

Instantly share code, notes, and snippets.

@imballinst
Last active July 9, 2017 15:37
Show Gist options
  • Save imballinst/864ca78b53912fb60f5cbad3bd9467da to your computer and use it in GitHub Desktop.
Save imballinst/864ca78b53912fb60f5cbad3bd9467da to your computer and use it in GitHub Desktop.
Alternative to Array.prototype.reduce()
const arr = [1, [2], [3, [[4], [5]]]];
let x = [];
function recurse(arr) {
let dest = [];
if (Array.isArray(arr)) {
arr.forEach(el => {
dest = dest.concat(recurse(el));
});
} else {
dest.push(arr);
}
return dest;
}
x = recurse(arr);
console.log(x);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment