Skip to content

Instantly share code, notes, and snippets.

@HichemBenChaaben
Created August 2, 2018 22:34
Show Gist options
  • Save HichemBenChaaben/f108b086eff839801cce5e13be5c288f to your computer and use it in GitHub Desktop.
Save HichemBenChaaben/f108b086eff839801cce5e13be5c288f to your computer and use it in GitHub Desktop.
// [1] => 1
// [1, [2,3,4]] => [1,2,3,4]
// [1, [2,3, [4,5]]] => [1,2,3,4,5]
const flatten = (arr) =>
arr.reduce((acc, next) =>
acc.concat(Array.isArray(next) ? flatten(next) : next), []);
const data = [1, [2, 3, [4, 5]]];
flatten(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment