Created
November 30, 2018 15:00
-
-
Save brenoferreira/aa9019edddbbc56a358c2eba3ae17e49 to your computer and use it in GitHub Desktop.
Flattens a deeply nested array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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