我有一个 main 函数,它执行一系列操作,并将输出与在各个变量中声明的其他多个值一起推送到数组中。然后我需要将它们传递给另一个子函数以进行其他一系列操作,然后返回更新后的数组。我意识到,如果我只传递数组名称,就像我将单个变量作为参数传递给另一个函数一样,它是行不通的。function mainfn(){var sheet=SpreadsheetApp.openById("THIS_SHEET_ID").getSheetByName("wshop");var sheetdata=sheet.getDataRange().getValues();var lrow=sheet.getLastRow();var var1=sheetdata[lrow-1][2];var var2=sheetdata[lrow-1][3];var arr1=[],arr2=[];//some operationsAarr1.push(1,"str1","3%",5,6,"str2"); //output OpsA being stored in array named arr1//some operationsBarr2.push(3,5,6,8,0,1); //output OpsB being stored in array named arr2mysubfunction(var1,var2,arr1,arr2); // calling subfunction and passing argumentsvar arr3=[],var arr4=[];var arr3=mysubfunction()[0]; // supposedly getting the arr1 updated elements and put into arr3var arr4=mysubfunction()[1]; // supposedly getting the arr2 updated elements and put into arr4// do some other stuff over the array elements}function mysubfunction(var1,var2,arr1,arr2){//some operationsC that updates the elements in both arraysvar result=[arr1,arr2];return result;}请帮助更正我关于传递和返回多个数组的代码。谢谢
1 回答
慕的地10843
TA贡献1785条经验 获得超8个赞
使用解构赋值:
var [arr3, arr4] = mysubfunction(var1,var2,arr1,arr2);// calling subfunction and getting the returned arrays
或者,
var resultArr = mysubfunction(var1,var2,arr1,arr2);
var arr3 = resultArr[0];
var arr4 = resultArr[1];
添加回答
举报
0/150
提交
取消