'get value from next index of object
I'm looping through an object. The loop is working perfectly fine, but after the number of value comes to zero it's jumping to the next key.
I want after iterating from the each key it should go to next one.
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);
Below is expected output
Expected output
{
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
