如何在JavaScript中仅迭代部分2D数组?

时间:2015-11-18 15:25:29

标签: javascript arrays algorithm

我有2D数组

var arr = [[0,  1,  2,  3,  4,  5],
           [6,  7,  8,  9,  10, 11],
           [12, 13, 14, 15, 16, 17],
           [18, 19, 20, 21, 22, 23],
           [24, 25, 26, 27, 28, 29],
           [30, 31, 32, 33, 34, 35]];

现在我们将其划分为更小的部分,如2x2。如何仅对此数组的部分(块)进行迭代,例如:索引为arr[2][4]arr[2][5]arr[3][4]arr[3][5]的项?

修改

似乎问题不容易理解。我想迭代块。

var blocks = 9;
var output = '';
for( var block = 0; block < blocks; block++) { 
  // actual iteration over array
  for(var i = ... ) {
    for(var j = ... ) {
      output += arr[i][j] + ' ';
    }
  }
  console.log(output);
  output = '';
}

预期输出为:

0 1 6 7
2 3 8 9
4 5 10 11
12 13 18 19
14 15 20 21
16 17 22 23
24 25 30 31
26 27 32 33
28 29 34 35

4 个答案:

答案 0 :(得分:0)

你需要嵌套for循环。

var arr = [[0,  1,  2,  3,  4,  5],
       [6,  7,  8,  9,  10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]];

function loopThrough() {
    for(int i = 0; i < arr.length; i++) {
        // this will give you arr[0], arr[1], etc.
        for(int j = 0; j < arr.length; j++) {
            // this will give you arr[0][0], arr[0][1], etc.
            console.log(arr[i][j]);
        }
    }
}

loopThrough();

如果您希望仅循环过去2,请设置j] = arr[i].length-2

<script type="text/javascript">

var arr = [[0,  1,  2,  3,  4,  5],
       [6,  7,  8,  9,  10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]];

function loopThrough() {
    for(var i = 0; i < arr.length; i++) {
        // this will give you arr[0], arr[1], etc.
        for(var j = arr[i].length-2; j < arr[i].length; j++) {
            // this will give you arr[0][0], arr[0][1], etc.
            console.log(arr[i][j]);
        }
    }
}

loopThrough();

</script>

答案 1 :(得分:0)

您希望按顺序访问这些索引:

arr[0][0] arr[0][1] arr[1][0] arr[1][1]
arr[0][2] arr[0][3] arr[1][2] arr[1][3]
[...]
arr[0][y-1] arr[0][y] arr[1][y-1] arr[1][y] // first two lines processed
arr[2][0] arr[2][1] arr[3][0] arr[3][1]     // we continue with the next two lines
[...]
arr[2][y-1] arr[2][y] arr[3][y-1] arr[3][y]
[...]
arr[x-1][0] arr[x-1][1] arr[x][0] arr[x][1]
[...]
arr[x-1][y-1] arr[x-1][y] arr[x][y-1] arr[x][y]

正如你所看到的,我们有两个迭代级别:第一个迭代线,跳过每一个,第二个遍历列,跳过每个列。

以下代码应实现此目的:

for (var x=0; x < arr.length; x+=2) {
  for (var y=0; y < arr[x].length; y+=2) {
    console.log(arr[x][y] + " " + arr[x][y+1] + " " + arr[x+1][y] + " " + arr[x+1][y+1]);
  }
}

你会注意到我们将迭代变量增加了两个循环,因为内部块一次消耗两列和一行。

答案 2 :(得分:0)

你需要一个带有偏移内循环的循环来迭代最后两个位置。这样的东西可行,你可以在这个js小提琴上看到结果:https://jsfiddle.net/0wfysb38/3/

var arr = [[0,  1,  2,  3,  4,  5],
           [6,  7,  8,  9,  10, 11],
           [12, 13, 14, 15, 16, 17],
           [18, 19, 20, 21, 22, 23],
           [24, 25, 26, 27, 28, 29],
           [30, 31, 32, 33, 34, 35]];



for(i = 0; i < arr.length - 1; i=i+2) {
    for(j = 0; j < arr[i].length - 1; j=j+2) {
        strOut = '';
        strOut = strOut + ', ' +arr[i][j];
        strOut = strOut + ', ' +arr[i][j+1];
        strOut = strOut + ', ' +arr[i+1][j];
        strOut = strOut + ', ' +arr[i+1][j+1];
        strOut = strOut.substring(1,strOut.length)
        console.log(strOut);
    }
}

编辑:更新为代码以生成与预期输出匹配的结果。关键是将循环迭代2,并通过偏移搜索数组中的下一个位置。这不是很灵活,这个循环的数组应该适当地构造。

答案 3 :(得分:0)

function subblock_array(arr, startX, endX, startY, endY) {  
    var subArr = [];
    for (var ii=startX; ii<endX; ii++) {
        subArrY = [];
        for (var jj=startY; jj<endY; jj++) {
            subArrY.push(arr[ii][jj]);
        }
        subArr.push(subArrY);
    }
    return subArr;
}

subblock_array(arr, 2, 4, 4, 6)