javascript var内有字符串问题的for循环

bgtovc5b  于 2022-11-20  发布在  Java
关注(0)|答案(1)|浏览(104)

'''

如何创建一个循环来替换javascript中的此代码:

我不知道为什么循环给予我一个错误。
你能帮我吗?

  • 谢谢-谢谢
var data1 = json2[(Math.floor(numero[1]*24))];
var data2 = json2[(Math.floor(numero[2]*24))];
var data3 = json2[(Math.floor(numero[3]*24))];
var data4 = json2[(Math.floor(numero[4]*24))];
var data5 = json2[(Math.floor(numero[5]*24))];

document.getElementById("wordi1").innerHTML = data1;
document.getElementById("wordi2").innerHTML = data2;
document.getElementById("wordi3").innerHTML = data3;
document.getElementById("wordi4").innerHTML = data4;
document.getElementById("wordi5").innerHTML = data5;

//This loop does not work
 for (let i = 1; i < 7; i++) {
var data+i = json2[(Math.floor(numero[i]*24))];
document.getElementById("wordi"+i).innerHTML = data+i ;}
t40tm48m

t40tm48m1#

"试试这个"

const dataArr = [];

for (let i = 1; i < 7; i++) {
   const data = json2[(Math.floor(numero[i]*24))];
   document.getElementById("wordi"+i).innerHTML = data;
   dataArr.push(data); 
}

console.log(dataArr); // this should have all your data values

相关问题