Last active
November 28, 2019 09:53
-
-
Save davidecavaliere/38c050d5f189c826b08f2832804d2c8f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
For more info please head to https://github.com/davidecavaliere/flatten | |
*/ | |
function* flatten([head, ...tail]) { | |
if (Array.isArray(head)) { | |
yield* flatten(head); | |
} else { | |
yield head; | |
} | |
if (tail.length > 0) { | |
yield* flatten(tail); | |
} | |
} | |
var arr = [[1, 2], 2, [3, 4, [5, 6]]]; | |
const flattened = [...flatten(arr)]; | |
console.log('flattened', flattened); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment