class DeeplyNestedIterator { constructor(nestedList) { this.gen = this.listGenerator(nestedList); this.nextVal = this.gen.next(); } hasNext() { return !this.nextVal.done; } getNext() { const val = this.nextVal.value; this.nextVal = this.gen.next(); return val; } *listGenerator(list) { for (const el of list) { if (Array.isArray(el)) yield* this.listGenerator(el); else yield el; } } }