Skip to content

Instantly share code, notes, and snippets.

@ashwell ashwell/flatten.js
Last active Jan 28, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.