如何使用谷歌应用脚本将值添加到带有for循环的二维数组



有人可以给我看一些简单的示例来向二维数组添加值吗?

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

预期行为:

整体值[[0],[0]] = 0, 整体值[[0],[1]] = 1, 整体值[[0],[2]

] = 2,

整体值[[1],[0]] = 0, 全值[[1],[1]] = 1,全值[[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]]);
}

希望这能有所帮助。

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]);
}

最新更新