Skip to content

Instantly share code, notes, and snippets.

@zmts
Last active January 11, 2019 16:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zmts/31da237511c4fe5d6660f2cd9e4c9334 to your computer and use it in GitHub Desktop.
Save zmts/31da237511c4fe5d6660f2cd9e4c9334 to your computer and use it in GitHub Desktop.
Flat Array

Flatten array in JS

[1, 2, 3, [4, 5, [6, 7]]].flat() // >> [1, 2, 3, 4, 5, [6, 7]]
[1, 2, 3, [4, 5, [6, 7]]].reduce((acc, next) => acc.concat(next), []) // >> [1, 2, 3, 4, 5, [6, 7]]

// deep flat
[1, 2, 3, [4, 5, [6, 7]]].flat(Infinity) // >> [1, 2, 3, 4, 5, 6, 7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment