Loops 比较两个列表,并使用谷歌应用程序脚本按顺序填充最终列表
我有以下格式的两个列表Loops 比较两个列表,并使用谷歌应用程序脚本按顺序填充最终列表,loops,for-loop,google-apps-script,Loops,For Loop,Google Apps Script,我有以下格式的两个列表 array_so_helper: [[01-0158969, 315.0], [01-0161045, 699.0], [01-0161046, 41.0], [01-0161047, 45.0]] array_so: [[01-0158969, null, 315.0, null], [01-0161045, null, 699.0, null], [01-0161047, null, 45.0,null]] 对于数组\u so\u帮助器中的每个第一
array_so_helper:
[[01-0158969, 315.0], [01-0161045, 699.0], [01-0161046, 41.0], [01-0161047, 45.0]]
array_so:
[[01-0158969, null, 315.0, null], [01-0161045, null, 699.0, null], [01-0161047, null, 45.0,null]]
对于数组\u so\u帮助器中的每个第一个元素,如果第二个列表(数组\u so)中存在匹配项,则将数组\u so中相应的子列表写入最终的\u列表。如果在array_so_helper中找到某个元素,但在array_so中找不到该元素,则将其以以下格式写入最终的_列表:
[01-0161046, null, 41.0, "yes"]
因此,最终的_列表必须是
[[01-0158969, null, 315.0, null], [01-0161045, null, 699.0, null],[01-0161046, null, 41.0, "yes"],, [01-0161047, null, 45.0,null]]
我尝试使用2个for循环来实现这一点,如下所示:
var array_so_final = [];
for(i=0; i<array_so_helper.length; i++){
elem = array_so_helper[i][0]
value = array_so_helper[i][1]
for(j=0;j<array_so.length;j++){
elem_helper_0 = array_so[j][0]
elem_helper_1 = array_so[j][1]
elem_helper_2 = array_so[j][2]
elem_helper_3 = array_so[j][3]
if(elem == elem_helper){
array_so_final.push([elem_helper_0,elem_helper_1,elem_helper_2,elem_helper_3])
}
}
}
var数组_so_final=[];
对于(i=0;i,当前代码中有许多未定义的变量。但您可以参考以下示例代码:
function compareList() {
var array_so_helper = [['01-0158969', 315.0], ['01-0161045', 699.0], ['01-0161046', 41.0], ['01-0161047', 45.0]];
var array_so = [['01-0158969', null, 315.0, null], ['01-0161045', null, 699.0, null], ['01-0161047', null, 45.0,null]];
var array_so_final = [];
for(i=0; i<array_so_helper.length; i++){
var elem = array_so_helper[i][0]
var value = array_so_helper[i][1]
Logger.log(elem);
//search for the element in the second list
var search = array_so.find(so =>{
return so[0]==elem;
});
Logger.log(search);
if(search){
//match found, add to final list
array_so_final.push(search);
}else{
//match not found
array_so_final.push([elem, null, value, "yes"]);
}
}
Logger.log("**FINAL**");
Logger.log(array_so_final);
}
5:37:58 AM Notice Execution started
5:37:59 AM Info 01-0158969
5:37:59 AM Info [01-0158969, null, 315.0, null]
5:37:59 AM Info 01-0161045
5:37:59 AM Info [01-0161045, null, 699.0, null]
5:37:59 AM Info 01-0161046
5:37:59 AM Info null
5:37:59 AM Info 01-0161047
5:37:59 AM Info [01-0161047, null, 45.0, null]
5:37:59 AM Info **FINAL**
5:37:59 AM Info [[01-0158969, null, 315.0, null], [01-0161045, null, 699.0, null], [01-0161046, null, 41.0, yes], [01-0161047, null, 45.0, null]]
5:37:59 AM Notice Execution completed