DEV Community

Discussion on: 9 Extremely Powerful JavaScript Hacks

Collapse
lukaszahradnik profile image
Lukáš Zahradník

Hi, good article.

I just wanted to point out, that there is a native way of flattening multidimensional arrays (and also faster).

var flat_entries = entries.flat(); 
Enter fullscreen mode Exit fullscreen mode

It also allows you to specify depth of flattening.

Collapse
razgandeanu profile image
Klaus Author

Interesting. Thank you for pointing that out.

Collapse
fifn2 profile image
Finn Wright

Only ES2019, tho 🤷

Collapse
lukaszahradnik profile image
Lukáš Zahradník • Edited on

Polyfills exist.
We cannot be stuck in the past so just we support all browsers (+ solutions in the article use spreading, which isn't supported by IE anyway)

Thread Thread
wkrueger profile image
Willian Krueger

i.e. has to add an extra babel plugin on the pipeline

Collapse
itepifanio profile image
Ítalo Epifânio

And if you use .Flat(Infinity) you flat all

Collapse
jstewart8053 profile image
jstewart8053 • Edited on

I noticed the same thing... adding

entries.flat(Infinity)
Enter fullscreen mode Exit fullscreen mode

will flatten the array down to a single array no matter how nested the array is :)
Oh... and also, super-helpful Post!