Skip to content

Instantly share code, notes, and snippets.

@myfreax
Forked from Integralist/flatten-array.js
Created September 4, 2018 07:40
Show Gist options
  • Save myfreax/b5c8cdb5421cee3adf3160a7373735d1 to your computer and use it in GitHub Desktop.
Save myfreax/b5c8cdb5421cee3adf3160a7373735d1 to your computer and use it in GitHub Desktop.
Array flatten function written in ES6 syntax
const flattenTco = ([first, ...rest], accumulator) =>
(first === undefined)
? accumulator
: (Array.isArray(first))
? flattenTco([...first, ...rest])
: flattenTco(rest, accumulator.concat(first))
const flatten = (n) => flattenTco(n, []);
console.log(flatten([[1,[2,[[3]]]],4,[5,[[[6]]]]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment