Skip to content

Instantly share code, notes, and snippets.

@fernandocamargo
Last active April 9, 2018 09:16
Show Gist options
  • Save fernandocamargo/2fcd6764bb26b65267515bbb4ed96df0 to your computer and use it in GitHub Desktop.
Save fernandocamargo/2fcd6764bb26b65267515bbb4ed96df0 to your computer and use it in GitHub Desktop.
const test = [[1, [2, [3]]], 4];
const dig = (stack, object) =>
Array.isArray(object) ? flatten(object, stack) : stack.concat(object);
const flatten = (input, output = []) => input.reduce(dig, output);
console.clear();
console.log(flatten(test)); // [1, 2, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment