Skip to content

Instantly share code, notes, and snippets.

@alexeykomov
Created October 4, 2016 15:21
Show Gist options
  • Save alexeykomov/9ba4ae71ec6e3e361077359ef1fdfb76 to your computer and use it in GitHub Desktop.
Save alexeykomov/9ba4ae71ec6e3e361077359ef1fdfb76 to your computer and use it in GitHub Desktop.
//[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