Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda