Skip to content

Instantly share code, notes, and snippets.

@Raidus
Forked from Integralist/flatten-array.js
Created August 8, 2017 00:50
Show Gist options
  • Save Raidus/c9b23f0b140b5f89dd09c412a787ecb6 to your computer and use it in GitHub Desktop.
Save Raidus/c9b23f0b140b5f89dd09c412a787ecb6 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