如何将多个数组和单个 arg 的混合传递给另一个函数并返回它们(GAS/JS)?

我有一个 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 operationsA

arr1.push(1,"str1","3%",5,6,"str2"); //output OpsA being stored in array named arr1


//some operationsB

arr2.push(3,5,6,8,0,1); //output OpsB being stored in array named arr2


mysubfunction(var1,var2,arr1,arr2); // calling subfunction and passing arguments


var arr3=[],var arr4=[];


var arr3=mysubfunction()[0]; // supposedly getting the arr1 updated elements and put into arr3

var 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 arrays


var result=[arr1,arr2];

return result;

}

请帮助更正我关于传递和返回多个数组的代码。谢谢


ABOUTYOU
浏览 106回答 1
1回答

慕的地10843

使用解构赋值: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];
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript