Last active
January 28, 2016 22:45
-
-
Save ashwell/3dcc6702158604978d96 to your computer and use it in GitHub Desktop.
Flatten with Generators
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
export default function flatten(arrayOfArrays=[]){ | |
function* flatgen() { | |
for( let item of arrayOfArrays ) { | |
if ( Array.isArray( item )) { | |
yield* flatten(item) | |
} else { | |
yield item | |
} | |
} | |
} | |
return [...flatgen()]; | |
} | |
var flatArray = flatten([[1, [2]], [3], [4, 5], [[6, 7], 8], 9]); | |
console.dir( flatArray ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment