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

222
Views
¿Cómo reiniciar la iteración con el método Map.values()?

Quiero reiniciar la iteración después de que el iterador llegue al estado terminado. Solo mira el ejemplo:

 const newMap = new Map<string, string>([ ['key1', 'value1'], ['key2', 'value2'] ]); const iterator = newMap.values() // It can be newMap.entries() iterator.next().value // prints value1 iterator.next().value //prints value2 iterator.next().value //prints undefined

Solo quiero algo como:

 iterator.restart(); iterator.next().value // prints value1
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Llame .values (o el método que sea) nuevamente.

 const newMap = new Map([ ['key1', 'value1'], ['key2', 'value2'] ]); let iterator = newMap.values() // It can be newMap.entries() console.log(iterator.next().value) // prints value1 console.log(iterator.next().value) //prints value2 console.log(iterator.next().value) //prints undefined iterator = newMap.values() console.log(iterator.next().value) // prints value1

Los iteradores no son reutilizables; debe crearlos de nuevo cada vez que desee comenzar desde el principio.

about 4 years ago · Juan Pablo Isaza Report

0

Si desea repetir valores sin fin, puede crear una función de generador que almacene en caché los valores la primera vez y luego siga brindándole los valores almacenados en caché desde el principio para siempre:

 function* repeat(iterable) { const cache = []; //lazily supply the values from the iterable while caching them for (const next of iterable) { cache.push(next); yield next; } //delegate to the cache at this point while(true) yield* cache; } const newMap = new Map([ ['key1', 'value1'], ['key2', 'value2'] ]); const iterator = repeat(newMap.values()) // It can be newMap.entries() console.log(iterator.next().value) // prints value1 console.log(iterator.next().value) // prints value2 console.log(iterator.next().value) // prints value1 console.log(iterator.next().value) // prints value2 console.log(iterator.next().value) // prints value1 console.log(iterator.next().value) // prints value2

Alternativamente, si no desea almacenar en caché, puede hacer que la implementación de repeat() sea más simple tomando una función que le proporcione un iterable. Cada vez que lo llama, obtiene uno nuevo y lo delega para siempre:

 function* repeat(iterableSupplier) { //delegate to the value from the supplier while(true) yield* iterableSupplier(); } const newMap = new Map([ ['key1', 'value1'], ['key2', 'value2'] ]); const iterator = repeat(() => newMap.values()) // It can be newMap.entries() // give a function ^^^^^ console.log(iterator.next().value) // prints value1 console.log(iterator.next().value) // prints value2 console.log(iterator.next().value) // prints value1 console.log(iterator.next().value) // prints value2 console.log(iterator.next().value) // prints value1 console.log(iterator.next().value) // prints value2

Ver más sobre:

  • function* - funciones del generador
  • yield* - delegar a otro iterable
  • Iteradores y generadores : más información sobre el tema en general
about 4 years ago · Juan Pablo Isaza Report

0

Podría crear su propio iterador que, cuando se le solicite, vuelva a llamar a newMap.values():

 const newMap = new Map([ ['key1', 'value1'], ['key2', 'value2'] ]); const myIterator = (() => { let currentIterator = newMap.values(); return { next() { return currentIterator.next(); }, restart() { currentIterator = newMap.values(); } } })() console.log(myIterator.next().value) // prints value1 console.log(myIterator.next().value) //prints value2 console.log(myIterator.next().value) //prints undefined myIterator.restart(); console.log(myIterator.next().value) // prints value1

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!