Skip to content

Instantly share code, notes, and snippets.


Keybase proof

I hereby claim:

  • I am nateq314 on github.
  • I am nateq ( on keybase.
  • I have a public key ASCA3MG53QsWUcHzRsHGagZMkcdlhkKNC25-qenpKZ8jRgo

To claim this, I am signing this object:

nateq314 / flatten.ts
Last active March 21, 2019 12:16
non-mutating flatten()
View flatten.ts
* Return the flattened version of an arbitrarily deep nested array as a new
* array, keeping the original unchanged. It uses a simple recursive algorithm.
* Positives: clean, intuitive, doesn't mutate the argument
* Negatives: 1) probably uses more memory than alternative implementations.
* 2) recursive, so in theory more limited by system resources than
* an alternative non-recursive algorithm would be. Unlikely to
* be a problem in most use cases but if you had an array nested
* say 1 million levels deep, probably wouldn't want to use this