gpt4 book ai didi

Javascript 和 for 循环问题

转载 作者:行者123 更新时间:2023-11-30 08:46:12 26 4
gpt4 key购买 nike

我的代码有问题吗?我期待我的代码:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com