Skip to content

Instantly share code, notes, and snippets.

@ashwell
Last active January 28, 2016 22:45
Show Gist options
  • Save ashwell/3dcc6702158604978d96 to your computer and use it in GitHub Desktop.
Save ashwell/3dcc6702158604978d96 to your computer and use it in GitHub Desktop.
Flatten with Generators
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