Skip to content

Instantly share code, notes, and snippets.

@bfillmer
Created January 18, 2018 16:45
Show Gist options
  • Save bfillmer/ebddeb42c1eeba5ab4fa2fd830181188 to your computer and use it in GitHub Desktop.
Save bfillmer/ebddeb42c1eeba5ab4fa2fd830181188 to your computer and use it in GitHub Desktop.
Recursively Flatten an Array
// Expected input -> output
// [[1,2,[3]],4] -> [1,2,3,4]
const input = [[1,2,[3]],4]
const flatten = (array, base = []) => array.reduce(
(result, current) => ((Array.isArray(current)) ? flatten(current, result) : [...result, current])
, base)
// -> [1, 2, 3, 4]
console.log(flatten(input))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment