Reputation: 180
I just think it's weird that I haven't found any topic or question about this yet, on how to set a limit to a For...In loop.
I want to iterate throught the keys in a object of Objects , like below :
for(let month in responseObject['Monthly Time Series'])
{
console.log(month);
}
However I want to set a limit to this loop , like for example : the first 6 keys , or the first 12 keys of the object.
If I wanted to this with a for loop it would be easy , I would simply have to do this :
for (let i = 0 ; i < 12; i++)
{
console.log(responseObject['Monthly Time Series'][i];
}
That is not possible thought , since this is an object. What would the alternative be ?
Upvotes: 0
Views: 1702
Reputation: 45
Just stick a condition within the loop that will force exit the loop when you want to?
int x = 0;
let month in responseObject['Monthly Time Series'])
{
console.log(responseObject[month];
x = x+1;
if(x>6)
{
month = (whatever the maximum value of responseObject is)
}
}
Upvotes: 1