_.flatten

Flattens array a single level deep.

  1. // Underscore/Lodash
  2. _.flatten([1, [2, [3, [4]], 5]]);
  3. // => [1, 2, [3, [4]], 5]
  4.  
  5. // Native
  6. const flatten = [1, [2, [3, [4]], 5]].reduce( (a, b) => a.concat(b), [])
  7. // => [1, 2, [3, [4]], 5]
  8.  
  9. // Native(ES2019)
  10. const flatten = [1, [2, [3, [4]], 5]].flat()
  11. // => [1, 2, [3, [4]], 5]

Browser Support for Array.prototype.reduce()

ChromeEdgeFirefoxIEOperaSafari
46.0 ✔3.0 ✔9.0 ✔10.5 ✔4 ✔

Browser Support for Array.prototype.flat()

ChromeEdgeFirefoxIEOperaSafari
69 ✔62 ✔56 ✔12 ✔