Skip to content

Instantly share code, notes, and snippets.

@raviteja83
Created January 4, 2019 19:35
Show Gist options
  • Save raviteja83/382b8cc4bb5232b8f1c79897313d320d to your computer and use it in GitHub Desktop.
Save raviteja83/382b8cc4bb5232b8f1c79897313d320d to your computer and use it in GitHub Desktop.
Flatten nested js arrays
const arr = [[1,2,[3]],4];
const flatten = list => list.reduce(
(result, val) => result.concat(Array.isArray(val) ? flatten(val) : val), []
);
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