Created
February 15, 2021 10:12
-
-
Save jmporchet/7ff0d511805af1f7c1491fe14e97a381 to your computer and use it in GitHub Desktop.
Object iterator with a generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a more concise way of creating iterators than manually returning an object with a
next()
function embedded