How can I limit for-in to 5 loops even if there are more properties in the object?
for(property in object){
//do this stuff for the first 5 properties
}
Without counters:
Object.keys(object).slice(0,4).map((property) => {
// do something with property
})
You could use a counter. Something like this:
let counter = 0;
for(property in object)
{
if (counter >= 5){
break;
}
counter++;
}
You can use a break;
Like this
let props = 0;
for(property in object){
//do this stuff for the first 5 properties
props++;
if (props > 4)
break;
}