简体   繁体   English

尝试从数组访问元素时Javascript错误

[英]Error in Javascript while trying to access element from array

I'm working on a MineSweeper program, and here's my problem: 我正在开发MineSweeper程序,这是我的问题:

So I make a 10x10 grid full of MineSquare objects which I defined earlier. 因此,我制作了一个10x10的网格,其中充满了我之前定义的MineSquare对象。

var grid = [];
    for (var i=0; i<10; i++){
        grid.push([]);
        for (var j=0; j<10; j++){
            grid[i].push(new MineSquare())
        }
    }

Then I make an array "bomb_list" to create 20 unique locations where the bombs will be. 然后,我创建一个数组“ bomb_list”以创建炸弹所在的20个唯一位置。

var bomb_list = [];
var found;
var rand;
while (bomb_list.length < 20){
    found = false;
    rand = Math.floor(Math.random() * 100);
    for (var i=0; i<bomb_list.length; i++){
        if (bomb_list[i] === rand){found=true; break;}
    }
    if(!found){
        bomb_list.push(rand);
    }
}

Then I try to change the value of those MineSquare's to indicate they are bombs. 然后,我尝试更改那些MineSquare的价值,以表明它们是炸弹。

for(var x in bomb_list){
    grid[ x / 10 ][ x % 10 ].touching = -1;
}

I am getting an error that says "Uncaught TypeError: Cannot read property '1' of undefined" which I believe is being caused when I try to take grid[x / 10]. 我收到一个错误消息,提示“未捕获的TypeError:无法读取未定义的属性'1'”,我认为是在尝试获取grid [x / 10]时引起的。

Any reason why this is occuring? 为什么会这样呢?

X/10 will give you a float value, should use Math.ceil or Math.floor to get the nearest int as per your logic. X/10将为您提供一个浮点值,应根据您的逻辑使用Math.ceilMath.floor来获取最接近的整数。 Then it should work. 然后它应该工作。

x/10 will produce a float. x/10将产生一个浮点数。 When x is 1 , it will be 0.1 . x1 ,它将为0.1 There is no index in grid of 0.1 , so the result is undefined . 0.1 grid中没有索引,因此结果undefined That is why you get that error. 这就是为什么您会收到该错误。

You can Math.floor or parseInt depending on what you are trying to do: 您可以根据要执行的操作使用Math.floorparseInt

for(var x in bomb_list){
    grid[ parseInt(x / 10) ][ x % 10 ].touching = -1;
}

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

相关问题 尝试从Java类访问javascript时出现错误 - Getting an error while trying to access javascript from a java class 使用JavaScript从数组访问元素名称(无值) - Access element name (no value) from array with javascript 从Javascript函数访问PHP数组元素 - Access to a PHP array element from Javascript function 尝试从数组中删除元素并使数组在Javascript中的网页上“刷新” - Trying to remove an element from array AND have the array “refresh” on the webpage in Javascript 将元素从 mongodb 推送到数组时出错 - Error , while pushing element from mongodb to an array 同时在数组中添加一个元素,在javascript中给出错误 - while adding one more element in array giving error in javascript 为什么在尝试从 firestore 中删除数组元素时出现权限不足错误,但我可以删除整个文档 - Why do I get an insufficient permissions error while trying to delete an array element from firestore, yet I can delete the entire document 尽管进行检查,但访问javascript数组元素时出错 - Error while accessing javascript array element inspite of having checks 尝试从对象数组中访问javascript函数 - Trying to access javascript function from within array of objects Javascript - 尝试使用 Array.lenght 访问 foreach 循环中每个数组的最后一个元素 - Javascript - Trying to use Array.lenght to access last element of each array in a foreach loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM