Created
October 4, 2016 15:21
-
-
Save alexeykomov/9ba4ae71ec6e3e361077359ef1fdfb76 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] | |
function 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