我正在编写一个google工作表扩展程序。基本上,脚本会查看如下表: “A”| “B”| “C”| “E〜F”
对于第四列中“〜”分割的每个字符串,我需要创建两个arrray,以便最后一列是E或F,但其余值是相同的,如下所示: [[ “A”, “B”, “C”, “E”],[ “A”, “B”, “C”, “F”]] 现在,理论上这应该很简单:我将最后一列拆分并循环。但是,当我追加我的数组时,它会覆盖第二个值,让我: [[ “A”, “B”, “C”, “F”],[ “A”, “B”, “C”, “F”]。 请注意,E列将来会有不确定数量的元素。 我感谢任何帮助!
var rangeArr=[["BF-A", "true", 'A kind reminder to make a weather report if you are on duty today', 'Something','2.0', 'Humphrey Bogart~James Dean'], ['BF-B', 'true', 'Kindly report on the condition of the BF now', 'Something else', '4.0', 'Humphrey Bogart~James Dean'], ['R-A6', 'true','A kind reminder to report on the reservoir level', 'Something completely different', '1.0', 'Angela Merkel']];
var newArr=[[]];
var count=0;
for(i in rangeArr){
var str=rangeArr[i][5];
var places=str.split("~");
var otherThings=rangeArr[i];
otherThings.pop()
for (a in places){
otherThings[5]=places[a]
var nextThing=otherThings
newArr[count]=nextThing;
count=count+1;
}
}
alert(newArr)
答案 0 :(得分:1)
我找到了解决方法。我完全不明白为什么以前的代码不起作用,这可能反映了我对JS数组缺乏了解。我只是从两个不同数组的元素创建一个数组,而不是将一个元素附加到原始数组。
var rangeArr=[["BF-A", "true", 'A kind reminder to make a weather report if you are on duty today', 'Something','2.0', 'Humphrey Bogart~James Dean'], ['BF-B', 'true', 'Kindly report on the condition of the BF now', 'Something else', '4.0', 'Humphrey Bogart~James Dean'], ['R-A6', 'true','A kind reminder to report on the reservoir level', 'Something completely different', '1.0', 'Angela Merkel']];
var newArr=[[]];
var count=0;
for(i=0; i<rangeArr.length; i++){
var str=rangeArr[i][5];
var places=str.split("~");
var otherThings=rangeArr[i];
otherThings.pop()
for (a=0; a<places.length; a++){
newArr[newArr.length]=[otherThings[1],otherThings[2],otherThings[3],otherThings[4],places[a]]
count=count+1;
}
}