Skip to content

Instantly share code, notes, and snippets.

@dfernandeza
Last active April 18, 2018 11:10
Show Gist options
  • Save dfernandeza/50319675fbef639439348946e4bb08f5 to your computer and use it in GitHub Desktop.
Save dfernandeza/50319675fbef639439348946e4bb08f5 to your computer and use it in GitHub Desktop.
Flatten an array of arbitrarily nested arrays of integers into a flat array of integers
let arr = [[1, 2, [3]], 4];
let flatten = a => a.reduce((acc, v) => Array.isArray(v) ? [...acc, ...flatten(v)] : [...acc, v], []);
console.log(flatten(arr)); // [1, 2, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment