Created
October 4, 2016 15:17
-
-
Save alexeykomov/9cc78dc5b93ff62ecba6910b253d804e to your computer and use it in GitHub Desktop.
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
//[1, [2, [3, 4], 5], 6, [7]] | |
// -> | |
//[1, 2, 3, 4, 5, 6, 7] | |
const reducer = (acc, newElement) => { | |
if (Array.isArray(newElement)) { | |
return acc.concat(newElement.reduce(reducer, [])); | |
} else { | |
return acc.concat([newElement]); | |
} | |
} | |
console.log([1, [2, [3, 4], 5], 6, [7]].reduce(reducer, [])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment