我的代码有问题吗?我期待我的代码:
years=new Array();
for (i = 0; i < 5; ++i) {
for (j = 1; j < 13; ++j) {
player.push(Math.round( nestedData[i].value[j] ))
}
years.push(player)
}
console.log(years)
打印类似的东西:
[array[12],array[12],array[12],array[12]]
但我得到的结果是:
[array[60],array[60],array[60],array[60]]
最佳答案
在第一个 for 循环中创建一个新的玩家数组。您的代码的问题是值被插入同一个数组实例。
var years = [];
for (i = 0; i < 5; ++i) {
var player = [];
for (j = 1; j < 13; ++j) {
player.push(Math.round( nestedData[i].value[j] ))
}
years.push(player)
}
console.log(years)
关于Javascript 和 for 循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21867348/