Skip to content

Instantly share code, notes, and snippets.

@alexeykomov
Created October 4, 2016 15:17
Show Gist options
  • Save alexeykomov/9cc78dc5b93ff62ecba6910b253d804e to your computer and use it in GitHub Desktop.
Save alexeykomov/9cc78dc5b93ff62ecba6910b253d804e to your computer and use it in GitHub Desktop.
//[1, [2, [3, 4], 5], 6, [7]]
// ->
//[1, 2, 3, 4, 5, 6, 7]
const reducer = (acc, newElement) => {
if (Array.isArray(newElement)) {
return acc.concat(newElement.reduce(reducer, []));
} else {
return acc.concat([newElement]);
}
}
console.log([1, [2, [3, 4], 5], 6, [7]].reduce(reducer, []));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment