Skip to content

Instantly share code, notes, and snippets.

@zmts
zmts / flat.md
Last active January 11, 2019 16:28
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]

FWIW: I (@rondy) am not the creator of the content shared here, which is an excerpt from Edmond Lau's book. I simply copied and pasted it from another location and saved it as a personal note, before it gained popularity on news.ycombinator.com. Unfortunately, I cannot recall the exact origin of the original source, nor was I able to find the author's name, so I am can't provide the appropriate credits.


Effective Engineer - Notes

What's an Effective Engineer?