反应俄罗斯方块。函数总是返回false

时间:2019-09-29 18:40:29

标签: javascript reactjs

在我的react项目中,我有2D数组,其中包含数字。 我还具有collisionCheck函数,该函数遍历它并检查某些值。我希望它返回true并在找到非零值时退出函数,但是它总是返回false。有人可以帮我理解为什么吗?

collisionCheck = (yPos, xPos) => {
        this.state.board.forEach((row, y) => row.forEach((value, x) => {
            if((y === yPos && x === xPos && value != 0)) return true; 

        }))
    }
this.state = {
            board: [
                [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 3, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
            ],

1 个答案:

答案 0 :(得分:4)

forEach的呼叫更改为some

collisionCheck = (yPos, xPos) => {
  return this.state.board.some((row, y) => row.some((value, x) => (y === yPos && x === xPos && value != 0)));
}