Skip to content

Instantly share code, notes, and snippets.

@zhuzhuaicoding
Created January 19, 2017 04:09
Show Gist options
  • Save zhuzhuaicoding/4d1c4fcbeacf8d980e82df477e25cce8 to your computer and use it in GitHub Desktop.
Save zhuzhuaicoding/4d1c4fcbeacf8d980e82df477e25cce8 to your computer and use it in GitHub Desktop.
function* iterTree (tree) {
if (Array.isArray(tree)) {
for (var i = tree.length - 1; i >= 0; i--) {
yield* iterTree(tree[i])
}
} else {
yield tree
}
}
const tree = [ 'a', ['b', 'c'], ['d', 'e'] ];
for(let x of iterTree(tree)) {
console.log(x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment