为什么这个数组在 React 中是未定义的?
Why is this array undefined in React?
我有以下代码,用于在扫雷游戏中设置地雷,但我一直在 plantMines() 函数中遇到错误,"TypeError: data[randomx] is undefined"。我是 React 的新手,所以也许我遗漏了一些简单的东西,但我就是看不出问题出在哪里。
initBoardData(height, width, mines) {
let data = this.createEmptyArray(height, width);
data = this.plantMines(data, height, width, mines);
return data;
}
// Create array of grid
createEmptyArray(height, width) {
let data = [];
for (let i = 0; i < height; i++) {
data.push([]);
for (let j = 0; j < width; j++) {
data[i][j] = {
x: i,
y: j,
isMine: false,
neighbour: 0,
isRevealed: false,
isEmpty: false,
isFlagged: false
};
}
}
return data;
}
// Place mines on board
plantMines(data, height, width, mines) {
let randomx, randomy, minesPlanted = 0;
while (minesPlanted < mines) {
randomx = this.getRandomNumber(width);
randomy = this.getRandomNumber(height);
if (!(data[randomx][randomy].isMine)) { //--ERROR ON THIS LINE
data[randomx][randomy].isMine = true;
minesPlanted++;
}
}
return (data);
}
getRandomNumber(dimension) {
return Math.floor((Math.random() * 1000) + 1 % dimension);
}
编辑:codesandbox - https://codesandbox.io/s/minesweeper-luono?file=/src/Board.js
试试这样改
randomx = this.getRandomNumber(height);
randomy = this.getRandomNumber(width);
根据您的代码,您应该像这样访问数组:data[randomy][randomx]
您在 getRandomNumber 中有错误 - 缺少括号:
getRandomNumber(dimension) {
return Math.floor(((Math.random() * 1000) + 1) % dimension);
}
不过还有一个问题,也使用@dellink提供的解决方案:
randomx = this.getRandomNumber(height);
randomy = this.getRandomNumber(width);
您必须进行 BOTH 更改,因此它开始工作了!!
您正在尝试不存在数组的索引。因此;您收到 undefined
错误。
您可以使用下面或@Enchew 的回答。
getRandomNumber(dimension) {
return Math.floor(Math.random() * Math.floor(dimension));
}
我想通了。 height
和 width
未正确定义,因此 randomx
和 randomy
被视为字符串,而不是数字,因此数组定义错误。
我有以下代码,用于在扫雷游戏中设置地雷,但我一直在 plantMines() 函数中遇到错误,"TypeError: data[randomx] is undefined"。我是 React 的新手,所以也许我遗漏了一些简单的东西,但我就是看不出问题出在哪里。
initBoardData(height, width, mines) {
let data = this.createEmptyArray(height, width);
data = this.plantMines(data, height, width, mines);
return data;
}
// Create array of grid
createEmptyArray(height, width) {
let data = [];
for (let i = 0; i < height; i++) {
data.push([]);
for (let j = 0; j < width; j++) {
data[i][j] = {
x: i,
y: j,
isMine: false,
neighbour: 0,
isRevealed: false,
isEmpty: false,
isFlagged: false
};
}
}
return data;
}
// Place mines on board
plantMines(data, height, width, mines) {
let randomx, randomy, minesPlanted = 0;
while (minesPlanted < mines) {
randomx = this.getRandomNumber(width);
randomy = this.getRandomNumber(height);
if (!(data[randomx][randomy].isMine)) { //--ERROR ON THIS LINE
data[randomx][randomy].isMine = true;
minesPlanted++;
}
}
return (data);
}
getRandomNumber(dimension) {
return Math.floor((Math.random() * 1000) + 1 % dimension);
}
编辑:codesandbox - https://codesandbox.io/s/minesweeper-luono?file=/src/Board.js
试试这样改
randomx = this.getRandomNumber(height);
randomy = this.getRandomNumber(width);
根据您的代码,您应该像这样访问数组:data[randomy][randomx]
您在 getRandomNumber 中有错误 - 缺少括号:
getRandomNumber(dimension) {
return Math.floor(((Math.random() * 1000) + 1) % dimension);
}
不过还有一个问题,也使用@dellink提供的解决方案:
randomx = this.getRandomNumber(height);
randomy = this.getRandomNumber(width);
您必须进行 BOTH 更改,因此它开始工作了!!
您正在尝试不存在数组的索引。因此;您收到 undefined
错误。
您可以使用下面或@Enchew 的回答。
getRandomNumber(dimension) {
return Math.floor(Math.random() * Math.floor(dimension));
}
我想通了。 height
和 width
未正确定义,因此 randomx
和 randomy
被视为字符串,而不是数字,因此数组定义错误。