如何使用Google Apps脚本使用for循环将值添加到二维数组 [英] How To Add values to two-dimensional array with for loops using Google Apps Script
本文介绍了如何使用Google Apps脚本使用for循环将值添加到二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以给我展示一些简单的示例,将带有for循环的值添加到二维数组吗?
Could someone show me some simple examples to add values with for loops to a two-dimensional array?
下面是我完全错误的测试脚本.
My totally wrong test script is below.
预期的行为:
wholeValues [[0],[0]] = 0,wholeValues [[0],[1]] = 1,wholeValues [[0],[2]] = 2,
wholeValues[[0],[0]] = 0, wholeValues[[0],[1]] = 1, wholeValues[[0],[2]] = 2,
wholeValues [[1],[0]] = 0,wholeValues [[1],[1]] = 1,wholeValues [[1],[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]]);
}
推荐答案
希望这会有所帮助.
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]);
}
这篇关于如何使用Google Apps脚本使用for循环将值添加到二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文