【问题标题】:How To Add values to two-dimensional array with for loops using Google Apps Script如何使用 Google Apps 脚本通过 for 循环向二维数组添加值
【发布时间】:2019-03-22 20:49:30
【问题描述】:

有人能告诉我一些简单的例子,用 for 循环将值添加到二维数组吗?

下面是我完全错误的测试脚本。

预期行为:

wholeValues[[0],[0]] = 0,wholeValues[[0],[1]] = 1,wholeValues[[0],[2]] = 2,

wholeValues[[1],[0]] = 0,wholeValues[[1],[1]] = 1,wholeValues[[1],[2]] = 2 .....

function test() {
  var wholeValues = [[],[]];
  var value = [];
    for (var i = 0; i < 5; i++){                     
       for (var j = 0; j < 3; j++) {           
           wholeValues[[i],[j]] = value[j];
       }
    }

  Logger.log(wholeValues[[0],[1]]);
}

【问题讨论】:

    标签: arrays for-loop google-apps-script


    【解决方案1】:

    希望这会有所帮助。

    function test() {
    
      //2d array
      var wholeValues = [];
    
    
      for (var i = 0; i < 5; i++){  
    
        //create a 1D array first with pushing 0,1,2 elements with a for loop
        var value = [];
        for (var j = 0; j < 3; j++) {           
          value.push(j);
        }
        //pushing the value array with [0,1,2] to thw wholeValues array. 
        wholeValues.push(value);
      } // the outer for loop runs five times , so five the 0,1,2 with be pushed in to thewholevalues array by creating wholeValues[0][0],wholeValues[0][1]...till..wholeValues[4][2]
    
      Logger.log(wholeValues[0][1]);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-29
      • 1970-01-01
      • 2021-12-17
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-25
      相关资源
      最近更新 更多