使用Nested for循环尝试打印数据但不能正确打印。
在嵌套的第二个循环中,数据读取不正确,而是读取相同的3次并重复相同的
使用的程序:
let val1:string[]=['Jan','Feb','Mar']
let val2:string[]=['Jan','Feb','Mar']
console.log(val2[0])
console.log(val2[1])
console.log(val2[2])
for (var index = 0; index < val1.length; index++) {
let data1 = val1[index];
console.log("data1:"+data1)
for (var index2 = 0; index2 < val2.length; index2++) {
let data2 = val2[index2];
console.log("data2:"+data1)
}
}
0/P:
Jan
Feb
Mar
data1:Jan
data2:Jan
data2:Jan
data2:Jan
data1:Feb
data2:Feb
data2:Feb
data2:Feb
data1:Mar
data2:Mar
data2:Mar
data2:Mar
答案 0 :(得分:1)
检查你的代码:)
let data2 = val2[index2];
console.log("data2:"+data1)
到
let data2 = val2[index2];
console.log("data2:"+data2)