Skip to content

Instantly share code, notes, and snippets.

@matchilling
Created June 12, 2017 10:25
Show Gist options
  • Save matchilling/5f54bba65e35393837a02740798b57a0 to your computer and use it in GitHub Desktop.
Save matchilling/5f54bba65e35393837a02740798b57a0 to your computer and use it in GitHub Desktop.
flattenArray.js created by matchilling - https://repl.it/IhOJ/0
const arr = [
1, 2, 3, 4, 5, 6, [1, 2, 3, 4], [1, 2, 3, 4], [ [1, 2, [['x']], 4], [1, () => 2, 3, 4], [1, 2, {}, 4, [1, 2, 3, 4]]]
],
flatten = arr => arr.reduce((acc, val) => acc.concat(
Array.isArray(val) ? flatten(val) : val), []
);
console.log(
flatten(arr)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment