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

202
Views
obtener valor del siguiente índice del objeto

Estoy recorriendo un objeto. El ciclo funciona perfectamente bien, pero después de que el número de value llega a zero , salta a la siguiente key .

Quiero que después de iterar desde cada key , debería ir a la siguiente.

 let trackOfReaction = { heart: 5, fire: 4, clap: 0, wow: 2, like: 1, } setInterval(() => { for (const [key, value] of Object.entries(trackOfReaction)) { if (value !== 0) { trackOfReaction[key] = value - 1 const userReaction = { reaction: key, storedReaction: true, }; console.log(userReaction) break; } } }, 2000);

A continuación se espera la salida

Rendimiento esperado

 { reaction: "heart", storedReaction: true }, { reaction: "fire", storedReaction: true }, { reaction: "wow", storedReaction: true }, { reaction: "like", storedReaction: true }, { reaction: "heart", // here after completing of the first set heart should print again storedReaction: true }, { reaction: "fire", storedReaction: true }, { reaction: "wow", storedReaction: true }, { reaction: "heart", // like became 0 so it not print, loop goes again back to heart storedReaction: true },...so on
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Esto funciona

Tenga en cuenta el empalme para que el tiempo de espera ya no tarde más cuando los valores se vuelven 0

 const trackOfReaction = { heart: 5, fire: 4, clap: 0, wow: 2, like: 1, }; // filter the items so we do not even process "clap" const arr = Object.entries(trackOfReaction).filter(([key, value]) => value > 0); let cnt = 0; let tId = setInterval(() => { if (cnt >= arr.length) cnt = 0; const [key, value] = arr[cnt]; if (value === 0) { // no need to subtract cnt++ return; } const userReaction = { reaction: key, storedReaction: true, value: value }; console.log(userReaction); arr[cnt][1]--; // count down the entry if (arr[cnt][1] === 0) arr.splice(cnt,1); // shorten the array console.log(JSON.stringify(arr)) const cont = arr.reduce((acc, [key, value], i) => { acc += value; return acc; }, 0); if (cont === 0) { // stop when the array is empty or all values are 0 clearInterval(tId); console.log("stopped"); return; // stop } cnt++; // add one }, 1000);

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!