0

So I have multiple objects in my initial state numbered from EV1-EV15, how can I grab the data in a loop? Something like this:

    for (var i = 0; i < 15; i++) {
        percentage = (0.5 / this.state.EVi.timeToFull) * 100;
        newSOC = Math.round((this.state.EVi.soc + percentage) * 10) / 10;
        if (newSOC >= 100) {
            newSOC =  100;
        }
        array.push(newSOC);
    }
rrain
  • 61
  • 1
  • 9

1 Answers1

0

this.state.EVi looks specifically for a property called EVi in this.state. Instead, interpolate the index into a string and use that get the appropriate thing from state.

for (var i = 0; i < 15; i++) {
    const currentItem = `EV${i}`;
    percentage = (0.5 / this.state[currentItem].timeToFull) * 100;
    newSOC = Math.round((this.state[currentItem].soc + percentage) * 10) / 10;
    if (newSOC >= 100) {
        newSOC =  100;
    }
    array.push(newSOC);
}
larz
  • 5,724
  • 2
  • 11
  • 20