Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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_Multidimensional Array - Fatal编程技术网

Javascript 二维数组中连续值的匹配

Javascript 二维数组中连续值的匹配,javascript,arrays,multidimensional-array,Javascript,Arrays,Multidimensional Array,我试图找出如何在二维数组中找到3个或3个匹配的连续值。我想我已经计算出了水平和垂直匹配,但是如果相同的值出现在两个不同的行中,如下面的列表数组(myArray),我将如何匹配: 在这个特定的2D数组中,myArray[0][1]、myArray[0][2]和myArray[1][1]都与值2匹配 以下是我必须为水平和垂直匹配查找匹配项的JavaScript: for (i in myArray) { for (j in myArray[i]) { if (myArray[

我试图找出如何在二维数组中找到3个或3个匹配的连续值。我想我已经计算出了水平和垂直匹配,但是如果相同的值出现在两个不同的行中,如下面的列表数组(myArray),我将如何匹配:

在这个特定的2D数组中,myArray[0][1]、myArray[0][2]和myArray[1][1]都与值2匹配

以下是我必须为水平和垂直匹配查找匹配项的JavaScript:

for (i in myArray) {
    for (j in myArray[i]) {
        if (myArray[i][j] == myArray[i][j - 1] && myArray[i][j - 1] == myArray[i][j - 2] && myArray[i][j] != "-") {
            // do something
        }
        if (i > 1) {
            if (myArray[i][j] == myArray[i - 1][j] && myArray[i - 1][j] == myArray[i - 2][j] && myArray[i][j] != "-") {
                // do something
            }                           
        }
    }
}
我走对了吗?我猜我可以混合使用两个if语句来找到匹配项,但我对如何找到匹配项有点茫然

谢谢

测试每个元素周围的交叉

for (var y=0, yLen=myArray.length; y<yLen; y++){
  for (var x=0, xLen=myArray[y].length; x<xLen; x++){
      var matches = 0,
          testing = myArray[y][x];
      // test left
      if (x>0 && myArray[y][x-1] === testing) matches++;
      // test right
      if ((x<myArray[y].length-1) && myArray[y][x+1] === testing) matches++; 
      // test above
      if (y>0 && myArray[y-1][x] === testing) matches++; 
      // test below
      if ((y<myArray.length-1) && myArray[y+1][x] === testing) matches++; 

      if (matches>=2){
         console.log(y,x,' is the central or corner element of a 3-or-more group');
      }
  }
}
for(var y=0,yLen=myArray.length;y1.使用三个等号(==)2.你到底在问什么?你需要告诉是否存在水平和垂直匹配吗?给出所需输出的示例
for (var y=0, yLen=myArray.length; y<yLen; y++){
  for (var x=0, xLen=myArray[y].length; x<xLen; x++){
      var matches = 0,
          testing = myArray[y][x];
      // test left
      if (x>0 && myArray[y][x-1] === testing) matches++;
      // test right
      if ((x<myArray[y].length-1) && myArray[y][x+1] === testing) matches++; 
      // test above
      if (y>0 && myArray[y-1][x] === testing) matches++; 
      // test below
      if ((y<myArray.length-1) && myArray[y+1][x] === testing) matches++; 

      if (matches>=2){
         console.log(y,x,' is the central or corner element of a 3-or-more group');
      }
  }
}