Menu

gitpiper

deepFlatten javascript Code Snippet in 2024

arrayrecursionintermediate

Last Updated: 29 November 2024

Deep flattens an array.

  • Use recursion.
  • Use Array.prototype.concat() with an empty array ([]) and the spread operator (...) to flatten an array.
  • Recursively flatten each element that is an array.
const deepFlatten = arr => [].concat(...arr.map(v => (Array.isArray(v) ? deepFlatten(v) : v)));
deepFlatten([1, [2], [[3], 4], 5]); // [1, 2, 3, 4, 5]

javascript snippet similar to deepFlatten For You in November 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️