Skip to content

Instantly share code, notes, and snippets.

@pedro-victor
Created February 28, 2019 20:39
Show Gist options
  • Save pedro-victor/4f3556551a42f2e4858996029ad79b58 to your computer and use it in GitHub Desktop.
Save pedro-victor/4f3556551a42f2e4858996029ad79b58 to your computer and use it in GitHub Desktop.
Flatten multi-dimensional arrays in Javascript
const rec = (arr) => arr.reduce((a, b) => a.concat(Array.isArray(b) ? rec(b) : b), [])
rec([[1, 2, [3]], 4])
// [1, 2, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment