Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数组内界的条件_Javascript_Arrays - Fatal编程技术网

Javascript 数组内界的条件

Javascript 数组内界的条件,javascript,arrays,Javascript,Arrays,我有一个矩阵,我有一个函数,它使用以下代码随机选取数组中的一个元素: npcSelectShip() { let selectCol = () => { let colIndex = Math.floor(Math.random() * this.npcSelectionField.length); let selectCell = () => { let cellIndex = Math.floor(Math.rand

我有一个矩阵,我有一个函数,它使用以下代码随机选取数组中的一个元素:

npcSelectShip() {
    let selectCol = () => {
        let colIndex = Math.floor(Math.random() * this.npcSelectionField.length);
        let selectCell = () => {
            let cellIndex = Math.floor(Math.random() * this.npcSelectionField[colIndex].length);
            if (this.npcSelectionField[colIndex][cellIndex].isEmpty === false) {
                selectCell();
            } else {
                this.npcSelectionField[colIndex][cellIndex].isEmpty = false;
                this.pickDirection(this.npcSelectionField, colIndex, cellIndex);
            }

        }

        selectCell();
    }

    selectCol();
}
在此之后,我有另一个函数,搜索随机拾取元素的邻居(顶部、右侧、底部和左侧),随机拾取邻居并更改属性:

    pickDirection(field, col, cell) {
       let neighbors = [];
       neighbors.push(
           field[col - 1][cell],
           field[col + 1][cell],
           field[col][cell - 1],
           field[col][cell + 1]
       );
       let randDir = () => {
           let randIndex = neighbors[Math.floor(Math.random() * neighbors.length)];
           if (randIndex.isEmpty === false) {
               randDir();
           } else {
               randIndex.isEmpty = false;
           }
       }

       randDir();
}
我面临的问题是,当随机选取的元素的索引为0或等于数组长度时,因为如果它在索引-1或索引+1处选取一个邻居,它基本上“超出范围”,我会得到以下错误:

TypeError: Cannot read property 'isEmpty' of undefined
TypeError: Cannot read property '9' of undefined
有没有一种方法可以解决这个问题,而不必写一大堆ifs和ELSE

感谢您的帮助。

您可以使用和默认模式,该模式返回空数组

空数组与concat结合使用时充当中性值

var neighbors = [].concat(
       (field[col - 1] || [])[cell] || [],
       (field[col + 1] || [])[cell] || [],
       (field[col] || [])[cell - 1] || [],
       (field[col] || [])[cell + 1] || []
   );
或者使用包装器进行访问

function getCell(array, col, cell) {
    return (array[col] || [])[cell] || [];
}
用法

工作原理

(field[col - 1] || [])[cell] || []
它试图获得

 field[col - 1]
如果值是未定义的

 field[col - 1] || []
它返回一个带有LOCAL运算符的空数组。我们得到的要么是
字段[col-1]
的数组,要么是一个空数组
[]

对于下一个索引,我们使用相同的模式并检查

(field[col - 1] || [])[cell]
存在,如果不存在,则将另一个空数组作为结果

(field[col - 1] || [])[cell] || []
现在我们要么有一个真实值,比如一个对象,要么有一个空数组

这是必要的,因为空数组不会添加到具有的数组中

您可以使用和一个默认模式,该模式返回一个空数组

空数组与concat结合使用时充当中性值

var neighbors = [].concat(
       (field[col - 1] || [])[cell] || [],
       (field[col + 1] || [])[cell] || [],
       (field[col] || [])[cell - 1] || [],
       (field[col] || [])[cell + 1] || []
   );
或者使用包装器进行访问

function getCell(array, col, cell) {
    return (array[col] || [])[cell] || [];
}
用法

工作原理

(field[col - 1] || [])[cell] || []
它试图获得

 field[col - 1]
如果值是未定义的

 field[col - 1] || []
它返回一个带有LOCAL运算符的空数组。我们得到的要么是
字段[col-1]
的数组,要么是一个空数组
[]

对于下一个索引,我们使用相同的模式并检查

(field[col - 1] || [])[cell]
存在,如果不存在,则将另一个空数组作为结果

(field[col - 1] || [])[cell] || []
现在我们要么有一个真实值,比如一个对象,要么有一个空数组

这是必要的,因为空数组不会添加到具有的数组中


您基本上有两种选择:

第一个选项:不在第一行/最后一行/列拾取:

npcSelectShip() {
    let selectCol = () => {
        let colIndex = Math.floor(Math.random() * (this.npcSelectionField.length-2)) +1;
        let selectCell = () => {
            let cellIndex = Math.floor(Math.random() * (this.npcSelectionField[colIndex].length-2)) +1;
            if (this.npcSelectionField[colIndex][cellIndex].isEmpty === false) {
                selectCell();
            } else {
                this.npcSelectionField[colIndex][cellIndex].isEmpty = false;
                this.pickDirection(this.npcSelectionField, colIndex, cellIndex);
            }

        }

        selectCell();
    }

    selectCol();
}
(参见
Math.random
函数调用后的“-2”和“+1”)

将采用介于
楼层(0*(长度-2))+1=1
楼层(1*(长度-2))+1=长度-1

第二个选项:设置不作为邻居添加超出范围的内容的条件

pickDirection(field, col, cell) {
   let neighbors = [];

   if(col-1 >= 0) {
       neighbors.push(field[col - 1][cell]);
   }
   if(col < field.length) {
       neighbors.push(field[col + 1][cell]);
   }
   if(cell-1 >= 0) {
       neighbors.push(field[col][cell -1]);
   }
   if(cell < field[col].length) {
       neighbors.push(field[col][cell +1]);
   }

   let randDir = () => {
       let randIndex = neighbors[Math.floor(Math.random() * neighbors.length)];
       if (randIndex.isEmpty === false) {
           randDir();
       } else {
           randIndex.isEmpty = false;
       }
   }

   randDir();
}
pickDirection(字段、列、单元格){
让邻居=[];
如果(列1>=0){
push(字段[col-1][cell]);
}
if(列<字段长度){
push(字段[col+1][cell]);
}
如果(单元1>=0){
push(字段[col][cell-1]);
}
if(单元格<字段[col].长度){
push(字段[col][cell+1]);
}
让randDir=()=>{
设randIndex=neighbors[Math.floor(Math.random()*neighbors.length)];
if(randIndex.isEmpty==false){
randDir();
}否则{
randex.isEmpty=false;
}
}
randDir();
}

您基本上有两种选择:

第一个选项:不在第一行/最后一行/列拾取:

npcSelectShip() {
    let selectCol = () => {
        let colIndex = Math.floor(Math.random() * (this.npcSelectionField.length-2)) +1;
        let selectCell = () => {
            let cellIndex = Math.floor(Math.random() * (this.npcSelectionField[colIndex].length-2)) +1;
            if (this.npcSelectionField[colIndex][cellIndex].isEmpty === false) {
                selectCell();
            } else {
                this.npcSelectionField[colIndex][cellIndex].isEmpty = false;
                this.pickDirection(this.npcSelectionField, colIndex, cellIndex);
            }

        }

        selectCell();
    }

    selectCol();
}
(参见
Math.random
函数调用后的“-2”和“+1”)

将采用介于
楼层(0*(长度-2))+1=1
楼层(1*(长度-2))+1=长度-1

第二个选项:设置不作为邻居添加超出范围的内容的条件

pickDirection(field, col, cell) {
   let neighbors = [];

   if(col-1 >= 0) {
       neighbors.push(field[col - 1][cell]);
   }
   if(col < field.length) {
       neighbors.push(field[col + 1][cell]);
   }
   if(cell-1 >= 0) {
       neighbors.push(field[col][cell -1]);
   }
   if(cell < field[col].length) {
       neighbors.push(field[col][cell +1]);
   }

   let randDir = () => {
       let randIndex = neighbors[Math.floor(Math.random() * neighbors.length)];
       if (randIndex.isEmpty === false) {
           randDir();
       } else {
           randIndex.isEmpty = false;
       }
   }

   randDir();
}
pickDirection(字段、列、单元格){
让邻居=[];
如果(列1>=0){
push(字段[col-1][cell]);
}
if(列<字段长度){
push(字段[col+1][cell]);
}
如果(单元1>=0){
push(字段[col][cell-1]);
}
if(单元格<字段[col].长度){
push(字段[col][cell+1]);
}
让randDir=()=>{
设randIndex=neighbors[Math.floor(Math.random()*neighbors.length)];
if(randIndex.isEmpty==false){
randDir();
}否则{
randex.isEmpty=false;
}
}
randDir();
}

第二种方法是有意义的,尽管我仍然会在某些位置上获得TypeErrors扫描您是否检查数组是否包含任何未定义的
值?(或者可能我犯了一个错误,但找不到它;也许其他人会发现我的错误)是的,邻居数组有时确实包含一个未定义的值。原因是什么?我不知道,我需要知道你是如何构建
this.npcSelectionField
数组的:Pthis.npcSelectionField不包含任何未定义的值,它基本上是一个外部数组,包含填充了对象的数组。第二种方法很有意义,虽然我仍然在某些OCASION上获得TypeErrors扫描您是否检查您的数组是否包含任何
未定义的
值?(或者可能我犯了一个错误,但找不到它;也许其他人会发现我的错误)是的,邻居数组有时确实包含一个未定义的值。原因是什么?我不知道,我需要知道你是如何构建你的
this.npcSelectionField
数组的:Pthis.npcSelectionField不包含任何未定义的值,它基本上是一个外部数组,包含填充了对象的数组。很抱歉回复太晚,但我想了解它是如何工作的。你能解释一下(字段[col-1]| |[])[cell]| |[]背后的逻辑吗?感谢你的解释。很抱歉回复太晚,但我想了解这是怎么回事。你能解释一下(字段[col-1]| |【】)[cell]|【】背后的逻辑吗?请欣赏这个解释。