Skip to content

Instantly share code, notes, and snippets.

@jmporchet
jmporchet / index.js
Last active February 15, 2021 10:06
Object iterator property
const obj = {
sub: {
array: []
},
[Symbol.iterator]() {
let index = 0;
return {
next: () => {
// you could insert custom logic here, like stepping 3 by 3, filtering, etc.
if ( index >= this.sub.array.length ) return { done: true }
@jmporchet
jmporchet / index.js
Created February 15, 2021 10:12
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];
}
}
}