Skip to content

Instantly share code, notes, and snippets.

@ianstarz
Forked from shawndumas/recursiveReduce.js
Created June 22, 2016 22:27
Show Gist options
  • Save ianstarz/4c22a218fa3a410c8d404c4548abd5da to your computer and use it in GitHub Desktop.
Save ianstarz/4c22a218fa3a410c8d404c4548abd5da to your computer and use it in GitHub Desktop.
[0, ['a', 'b', 'c'], 2, 'd', 4, 5, 'f', 7, ['g', 'h'], 9].reduce(function rec (prev, curr) {
if (/array/i.test(curr.constructor)) {
return curr.reduce(rec, prev);
} else if (/string/i.test(curr.constructor)) {
prev.push(curr);
}
return prev;
}, []);
//["a", "b", "c", "d", "f", "g", "h"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment