Skip to content

Instantly share code, notes, and snippets.

@jmporchet
Created February 15, 2021 10:12
Show Gist options
  • Save jmporchet/7ff0d511805af1f7c1491fe14e97a381 to your computer and use it in GitHub Desktop.
Save jmporchet/7ff0d511805af1f7c1491fe14e97a381 to your computer and use it in GitHub Desktop.
Object iterator with a generator
const obj = {
sub: {
array: [1,2,3,4,5,6,7,8,9]
},
*[Symbol.iterator]() {
for (let i = 0; i < this.sub.length; i++) {
yield this.sub[i];
}
}
}
@jmporchet
Copy link
Author

This is a more concise way of creating iterators than manually returning an object with a next() function embedded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment