如何在for循环中构建数组数组?

时间:2018-06-18 14:14:56

标签: javascript for-loop

我想使用一个或多个for循环来构建matrix数组,但我并不完全理解如何实现'每一行本身都包含在一个数组中。通过循环构建它



var matrix = [
    [0, 0, 0, 1, 0],
    [1, 0, 0, 0, 1],
    [0, 0, 1, 0, 0],
];

var loopMatrix = []
var columns = 5
var rows = 3
for (var x = 0; x < columns; x++) {
  for (var y = 0; y < rows; y++) {
    var index = x * y
    if ((x == 3 && y == 0) || (x == 4 && y == 1) || (x == 0 && y == 1) || (x == 2 && y == 2)) {
      loopMatrix.push(1)
    } else {
      loopMatrix.push(0)
    }
  }
}

console.log(matrix) 
console.log(loopMatrix) // should be identical to matrix
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

这是你的代码:

var matrix = [
    [0, 0, 0, 1, 0],
    [1, 0, 0, 0, 1],
    [0, 0, 1, 0, 0],
];

var loopMatrix = []
var columns = 5
var rows = 3
for (var x = 0; x < columns; x++) {
  for (var y = 0; y < rows; y++) {
    var index = x * y
    if ((x == 3 && y == 0) || (x == 4 && y == 1) || (x == 0 && y == 1) || (x == 2 && y == 2)) {
      loopMatrix.push(1)
    } else {
      loopMatrix.push(0)
    }
  }
}

console.log(matrix) 
console.log(loopMatrix) // should be identical to matrix

以下是我的回答:

var matrix = [
    [0, 0, 0, 1, 0],
    [1, 0, 0, 0, 1],
    [0, 0, 1, 0, 0],
];

var loopMatrix = []
var columns = 5
var rows = 3
for (var x = 0; x < rows; x++) {   // swapped rows with columns.
  loopMatrix.push([]) // Added a new array instance here.
  for (var y = 0; y < columns; y++) {
    var index = x * y
    if ((x == 3 && y == 0) || (x == 4 && y == 1) || (x == 0 && y == 1) || (x == 2 && y == 2)) {
      loopMatrix[x].push(1) // reference x here because it exists by this point in the execution.
    } else {
      loopMatrix[x].push(0)  // reference x here because it exists by this point in the execution.
    }
  }
}

console.log(matrix) 
console.log(loopMatrix) // should be identical to matrix

当你看到我的回答时,不同之处在于我将它循环播放并推送了这个X.我保留了你所有的代码和内容。它起作用的原因是,由于X是0-> N,它将有一个进入数组部分的条目,因此它不会有任何索引超出边界错误。

根据我的解决方案,您现在可以通过以下方式轻松引用它:loopMatrix[row][col]其中row / col,0-indexed

答案 1 :(得分:0)

您应该构建一个数组数组。我认为你不需要在任何地方使用index。我跟着这个:

  1. 如果未定义当前行,请将其定义为数组。
  2. 如果值在我们的某个目标中,则将其设置为1.
  3. 否则,将其设置为0.
  4. 这是您需要注意的主要代码:

    var index = x * y;
    if (typeof loopMatrix[x] == "undefined")
      loopMatrix[x] = [];
    if ((x == 3 && y == 0) || (x == 4 && y == 1) || (x == 0 && y == 1) || (x == 2 && y == 2)) {
      loopMatrix[x][y] = 1;
    } else {
      loopMatrix[x][y] = 0;
    }
    

    完整代码:

    var matrix = [
      [0, 0, 0, 1, 0],
      [1, 0, 0, 0, 1],
      [0, 0, 1, 0, 0],
    ];
    
    var loopMatrix = [];
    var columns = 5;
    var rows = 3;
    for (var x = 0; x < rows; x++) {
      for (var y = 0; y < columns; y++) {
        var index = x * y;
        if (typeof loopMatrix[x] == "undefined")
          loopMatrix[x] = [];
        if ((x == 3 && y == 0) || (x == 4 && y == 1) || (x == 0 && y == 1) || (x == 2 && y == 2)) {
          loopMatrix[x][y] = 1;
        } else {
          loopMatrix[x][y] = 0;
        }
      }
    }
    
    console.log(matrix);
    console.log(loopMatrix);

    以上代码将为您提供:

    matrix     = [[0, 0, 0, 1, 0], [1, 0, 0, 0, 1], [0, 0, 1, 0, 0]]
    loopMatrix = [[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0]]
    

答案 2 :(得分:0)

如果您只想使用另一个相同的数组,只需使用slice methode

var matrix = [
[0, 0, 0, 1, 0],
[1, 0, 0, 0, 1],
[0, 0, 1, 0, 0]];

var loopMatrix = matrix.slice();

check slice documentation

答案 3 :(得分:0)

var matrix = [
[0, 0, 0, 1, 0],
[1, 0, 0, 0, 1],
[0, 0, 1, 0, 0],
];

var loopMatrix = []
var columns = 5
var rows = 3


for ( var x = 0; x < matrix.length; x++)
{
    for(var y = 0; y < matrix[x].length; y++)
    { 
   put your logic here that will set your 1's and 0's
   such as if x = 0 and y = 3 then make it a 1

   if (index === 3 || index === 5 || index === 9 || index === 12) {
     loopMatrix.push(1)
   } else {
     loopMatrix.push(0)
   }
}
} 


console.log(matrix) 
console.log(loopMatrix) // should be identical to matrix