Reputation: 671
I have an issue using .entries on an array to get index and value in a for of loop
ranks: any = {
total: 100,
percentages: [
{
percentage: 33
},
{
percentage: 33
},
{
percentage: 34
}
]
};
getMax(i: number){
var max: number = 100;
for (let [index,value] of this.ranks.percentages.entries()) {
console.log(index, value.percentage);
if(index !== i){
max -= value.percentage;
}
}
return max;
}
The console log is never logged in this case. I made a simple example that works so I must be missing something clearly obvious.
array = [
{ name:12 },
{ name:23 },
{ name:34 }
]
for (let [index, value] of array.entries()) {
console.log(index, value.name);
}
I'd normally try to console.log things to get at the issue but I'm not sure what to log or how I could use breakpoints since it skips over it.
Upvotes: 0
Views: 148
Reputation:
TS compiles the for
statement as
for (var _i = 0, _a = ranks.percentages.entries(); _i < _a.length; _i++) {
length
of an iterator is undefined, so the for loop becomes a no-op.
In other words, it does not seem to know that entries()
is yielding an iterable. Are you getting a compilation error about entries
?
If you compile down to ES6, this works.
If you just want to get your code working, then just rewrite it as a basic for loop.
Upvotes: 2