Skip to content

Instantly share code, notes, and snippets.

@brenoferreira
Created November 30, 2018 15:00
Show Gist options
  • Save brenoferreira/aa9019edddbbc56a358c2eba3ae17e49 to your computer and use it in GitHub Desktop.
Save brenoferreira/aa9019edddbbc56a358c2eba3ae17e49 to your computer and use it in GitHub Desktop.
Flattens a deeply nested array
function flatten(xs, output=[]) {
xs.map(x => {
if (Array.isArray(x)) {
flatten(x, output);
}
else {
output.push(x);
}
});
return output;
}
const a = [[1,2,[3]],4];
const f = flatten(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment