Javascript 动态Google应用程序脚本数组构造失败
从本质上讲,我正在查看先前创建的二维数组,并希望输出与另一个二维数组中的特定情况匹配的每个值Javascript 动态Google应用程序脚本数组构造失败,javascript,google-apps-script,google-sheets,dynamic-arrays,dynamically-generated,Javascript,Google Apps Script,Google Sheets,Dynamic Arrays,Dynamically Generated,从本质上讲,我正在查看先前创建的二维数组,并希望输出与另一个二维数组中的特定情况匹配的每个值 function FruitsToEat() { var fruits = [ ["Name", "TastesGood?", "ShouldEat?"], ["Apple", true, true], ["Orange", false, true], ["Grapes', true,
function FruitsToEat() {
var fruits = [
["Name", "TastesGood?", "ShouldEat?"],
["Apple", true, true],
["Orange", false, true],
["Grapes', true, true],
["Forbidden", true, false]
];
var EatThese = [];
var i = 0;
var rows = 0;
var columns = 0;
for (i = 0; i < fruits.length; i++) { //Check the entire Array
if (fruits[i][2] == true) { //If the "ShouldEat?" column = true
if (column != 2) { //Once column reaches 2, make a new row
EatThese[row][column] = fruits[i][0]; //Set the cell to the fruit name
column++; //Move to the right in the array
} else {
row++; //Move down one in the array
column = 0; //Move back to the left of the array
EatThese[row][column] = fruits[i][0]; //Set the cell to the fruit name
column++; //Move to the right in the array
}
}
}
return EatThese;
}
当我期待的时候
Apple Orange
Grapes
在各自的单元格中,以单张形式显示
所以我自己在打字时解决了这个问题。我将把这个留在这里,因为我在任何地方都找不到帮助
如果有人能告诉我,我仍然想知道为什么我最初的想法不起作用
function FruitsToEat() {
var fruits = [
["Name", "TastesGood?", "ShouldEat?"],
["Apple", true, true],
["Orange", false, true],
["Grapes', true, true],
["Forbidden", true, false]
];
var EatThese = []; //This is our main array and will be what we return.
var EatTheseTemp = []; //This is our temporary array. It will store each row of the main array until it is filled.
var i = 0;
var rows = 0;
var columns = 0;
for (i = 0; i < fruits.length; i++) { //Check the entire Array
if (fruits[i][2] == true) { //If the "ShouldEat?" column = true
if (column != 2) { //Our fill detection
EatTheseTemp[column] = fruits[i][0]; //Set the cell to the fruit name
column++; //Move to the right in the temporary array
} else {
EatThese[row] = EatTheseTemp; //Start constructing the main array
row++; //Prepare for the next fill
column = 0; //Move back to the left of the temporary array
EatTheseTemp = []; //Reset our temporary array
EatTheseTemp[column] = fruits[i][0]; //Set the cell to the fruit name
column++; //Move to the right in the temporary array
}
}
}
EatThese[row] = EatTheseTemp;
return EatThese;
}
函数FruitsToEat(){
风险值=[
[“姓名”,“味道好吗?”,“应该吗?”],
[“苹果”,真的,真的],
[“橙色”,假,真],
[“葡萄”,真的,真的],
[“禁止”,对,错]
];
var eatthes=[];//这是我们的主数组,将返回它。
var EATTESTEMP=[];//这是我们的临时数组。它将存储主数组的每一行,直到填充为止。
var i=0;
var行=0;
var列=0;
对于(i=0;i
基本上,我创建了两个数组。每次初始if语句返回true时,一个数组都会向其中添加片段。一旦嵌入的if语句显示此数组已填充,则将完整的行添加到主数组,然后擦除其他变量以重新开始。在for循环之外,您需要添加最后一行和两个d维度数组已完成。您的初始代码不起作用,因为您试图为
eatthes[0][0]
赋值。您已将eatthes
定义为数组,但未定义数组的元素…eatthes[0]
将是未定义的
,直到您为其分配了某些内容。在这种情况下,请将其设置为数组…eatthes[0]=[];
,然后您可以继续分配eatthes[0][0]
。可能存在重复的
function FruitsToEat() {
var fruits = [
["Name", "TastesGood?", "ShouldEat?"],
["Apple", true, true],
["Orange", false, true],
["Grapes', true, true],
["Forbidden", true, false]
];
var EatThese = []; //This is our main array and will be what we return.
var EatTheseTemp = []; //This is our temporary array. It will store each row of the main array until it is filled.
var i = 0;
var rows = 0;
var columns = 0;
for (i = 0; i < fruits.length; i++) { //Check the entire Array
if (fruits[i][2] == true) { //If the "ShouldEat?" column = true
if (column != 2) { //Our fill detection
EatTheseTemp[column] = fruits[i][0]; //Set the cell to the fruit name
column++; //Move to the right in the temporary array
} else {
EatThese[row] = EatTheseTemp; //Start constructing the main array
row++; //Prepare for the next fill
column = 0; //Move back to the left of the temporary array
EatTheseTemp = []; //Reset our temporary array
EatTheseTemp[column] = fruits[i][0]; //Set the cell to the fruit name
column++; //Move to the right in the temporary array
}
}
}
EatThese[row] = EatTheseTemp;
return EatThese;
}