Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
How to make the symbol.iterator resume when added a new item to the array

I print a list by iterating the Symbol.iterate object. I want it to resume printing when I add a new item to the array, but it doesn't work. I've tried some ways to do it but I couldn't. How can I overcome this problem?

let arr = [{
        name : "Computer",
        sold : 1000
    },
    {
        name: "Printer",
        sold: 250
    },
    {
        name: "Camera",
        sold:290
    },
    {
        name: "Gamepad",
        sold: 800
    },
    {
        name: "Keyboard",
        sold: 2100
    }
]

const iteratorArr = arr[Symbol.iterator]();

const listAll = () => {
    var it = iteratorArr.next()
    while(!it.done) {
        console.log(it.value) 
        it = iteratorArr.next()
    }
}

listAll()

arr.push({name:"Mouse",sold: 2900})
listAll()
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Array iterators (and really, any builtin iterators) do not support resumption after having been exhausted once. It is possible to create the iterator before adding values to the array, and still iterate them afterwards, but you must not have run the iterator to completion in between. Example:

const arr = ['a', 'b'];
const iter = arr.values();
console.log(iter.next().value); // not yet done
console.log(iter.next().value); // not yet done
arr.push('c');
console.log(iter.next().value); // not yet done
console.log(iter.next().done); // no value left, closing
arr.push('d');
console.log(iter.next().done); // still closed

To get the behavior you want, you'd need to implement your own iterator:

let arr = [
    { name : "Computer", sold : 1000 },
    { name: "Printer", sold: 250 },
    { name: "Camera", sold:290 },
    { name: "Gamepad", sold: 800 },
    { name: "Keyboard", sold: 2100 }
]

const iteratorArr = {
    index: 0,
    next() {
        const done = this.index >= arr.length
        return {done, value: done ? undefined : arr[this.index++]}
    },
    [Symbol.iterator]() { return this }
}

const listRemaining = () => {
    for (const value of iteratorArr) {
        console.log(value)
    }
    console.log(iteratorArr);
}

listRemaining()

arr.push({name:"Mouse",sold: 2900})
listRemaining()

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!