Created
May 2, 2017 01:39
-
-
Save tpgmartin/3a027d86ab80ef22e0568f81e65f5b8d to your computer and use it in GitHub Desktop.
Flatten array sample assuming input will only ever contain nested arrays of numbers
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(arr, flat) { | |
let output = !!flat ? flat : [] | |
arr.forEach((el) => { | |
if (typeof el === 'number') { | |
output.push(el) | |
} else { | |
flatten(el, output) | |
} | |
}) | |
return output | |
} | |
flatten([1, 2, [3, [4], 5, 6], 7]) // [1, 2, 3, 4, 5, 6, 7] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment