Skip to content

Instantly share code, notes, and snippets.

@davidecavaliere
Last active November 28, 2019 09:53
Show Gist options
  • Save davidecavaliere/38c050d5f189c826b08f2832804d2c8f to your computer and use it in GitHub Desktop.
Save davidecavaliere/38c050d5f189c826b08f2832804d2c8f to your computer and use it in GitHub Desktop.
/*
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