Skip to content

Instantly share code, notes, and snippets.

@wanderlima
Created January 2, 2019 23:07
Show Gist options
  • Select an option

  • Save wanderlima/cdf5c1941a478fbc046b659c489a85ac to your computer and use it in GitHub Desktop.

Select an option

Save wanderlima/cdf5c1941a478fbc046b659c489a85ac to your computer and use it in GitHub Desktop.
// Using ES6 reduce
const flatIt = arr => arr.reduce(
(a, b) => a.concat(Array.isArray(b) ? flatIt(b) : b), []
);
// Example
const arrayToFlat = [1,[2, 3],[4,[5, 6],[7, 8],[9,10,[11, 12]]]];
console.log(flatIt(arrayToFlat));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment