【发布时间】:2021-11-27 16:29:09
【问题描述】:
我从这段代码中得到了一个“TypeError: Cannot set property '0' of undefined”。谁能帮我这个!非常感谢
let array = [[]];
function generateField(width, height) {
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
array[i][j] = Math.floor(Math.random() * 3);
};
};
console.log(array);
return array;
}
generateField(5, 5);
【问题讨论】:
-
您有 2 个数组,您正在尝试写入两个以上的数组。您需要构建额外的数组。
标签: javascript arrays loops