简体   繁体   English

未捕获的TypeError:无法读取数组中未定义的属性“ 1”

[英]Uncaught TypeError: Cannot read property '1' of undefined in array

var grid = [];
    for (var i = 0;i < 6;i++)
    {
        for (var j = 0; j < 6; j++)
        grid[i] = [];
    }

    //Checking function(how many bombs near)
    function check(cx,cy)
    {
        var numb = 0;
        if (grid[cx][cy - 1] == "B") numb++;
        if (grid[cx][cy + 1] == "B") numb++;
        if (grid[cx - 1][cy] == "B") numb++;
        if (grid[cx + 1][cy] == "B") numb++;
        if (grid[cx - 1][cy - 1] == "B") numb++;
        if (grid[cx + 1][cy - 1] == "B") numb++;
        if (grid[cx - 1][cy + 1] == "B") numb++;
        if (grid[cx + 1][cy + 1] == "B") numb++;

        return numb;
    }** 

it gives me an error when i try to check every position that contains cx + or - 1, i tried to make the array in other ways but it didn't help. 当我尝试检查每个包含cx +或-1的位置时,它给了我一个错误,我尝试以其他方式制作数组,但没有帮助。 I try to make a minesweeper game, so here i check how many bombs are near each given idex 我尝试做一个扫雷游戏,所以在这里我检查每个给定的等离子点附近有多少枚炸弹

To prevent using indices of an array which have no array or value, you could use a function and hand over the array and indices and return true , if the value is 'B' , otherwise false . 为了避免使用没有数组或值的数组索引,可以使用函数并将数组和索引移交,如果值是'B'则返回true ,否则返回false

The check takes plase with in operator and a guard operator ( logical AND && ) which breaks on false and continues for true and checks then the value. 检查使用in运算符和后卫运算符( 逻辑AND && ),后者转为false并继续为true ,然后检查值。

function checkB(array, i, j) {
    return i in array && array[i][j] === 'B';
}

// call
if (checkB(grid, cx, cy - 1)) numb++;

check your function 检查你的功能

when call check(0,0) 通话检查时(0,0)

if (grid[cx][cy - 1] == "B") numb++;

grid[0][0-1]= 格[0] [0-1] =

0-1=?! 0-1 =?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法读取未定义的属性“未定义” - Uncaught TypeError: Cannot read property 'undefined' of undefined 数组内部循环引发未捕获的TypeError:无法读取未定义的属性“ 1” - loop inside an array throws Uncaught TypeError: Cannot read property '1' of undefined 数组迭代的“未捕获的类型错误:无法读取未定义的属性‘长度’” - "Uncaught TypeError: Cannot read property 'length' of undefined" for an array iteration Javascript中的数组错误:未捕获的TypeError:无法读取未定义的属性&#39;x&#39; - Array Error in Javascript: Uncaught TypeError: Cannot read property 'x' of undefined 未捕获的类型错误:无法读取数组 2d 上未定义的属性“0” - Uncaught TypeError: Cannot read property '0' of undefined on array 2d 未捕获的类型错误:无法读取 JS 中未定义错误的属性“数组” - Uncaught TypeError: Cannot read property 'Array' of undefined error in JS “未捕获的类型错误:无法读取未定义的属性‘0’”javascript 数组 - "Uncaught TypeError: Cannot read property '0' of undefined" javascript array 数组:未捕获的类型错误:无法读取未定义的属性“推送” - Array: Uncaught TypeError: Cannot read property 'push' of undefined 未捕获的TypeError:无法读取未定义的属性“长度”(数组) - Uncaught TypeError: Cannot read property 'length' of undefined (array) 未捕获的TypeError:无法读取3D数组中未定义的属性“ 0” - Uncaught TypeError: Cannot read property '0' of undefined in 3d array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM