因此,我在EV1-EV15编号的初始状态中有多个对象,如何在循环中获取数据?这样的东西:
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);
}
this.state.EVi
专门寻找this.state
中称为EVi
的属性。相反,将索引插入字符串中,并使用从状态获得适当的东西。
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);
}