在Excel脚本/Javascript中将数组的单个数组添加到多维数组的数组中
Adding a single array of an array into an array of a multidimensional array in Excel Scripts/ Javascript
希望将单个数组添加到多维数组或数组数组中。
例如:
Data = [["A","B","C","D"]];
NewData =[[1,2,3,4],[5,6,7,8],[9,10,11,12]];
CombinedData = [];
for(i=0; i< NewData.length; i++){
CombinedData.push(...Data,NewData[i]);
}
但是上面的结果是这样的:
"CombinedData =[["A","B","C","D"],[1,2,3,4],["A","B","C","D"], [5,6,7,8],["A","B","C","D"],[9,10,11,12],["A","B","C" "D"]...]"
我要找的是这个:
"CombinedData =[["A","B","C","D",1,2,3,4],["A","B","C","D",5,6 ,7,8],["A","B","C","D",9,10,11,12],]"
这是在 ExcelScripts 或办公脚本中,我知道这是很新的但只是在寻找某种方向。
您需要展开两个数组并用括号括起来以创建一个新数组。
const Data = ["A","B","C","D"];
const NewData =[[1,2,3,4],[5,6,7,8],[9,10,11,12]];
var CombinedData = [];
for(i=0; i< NewData.length; i++){
CombinedData.push([...Data,...NewData[i]]);
}
console.log(CombinedData);
此外,您可以只使用 map 而不是 for 循环:
const Data = ["A","B","C","D"];
const NewData =[[1,2,3,4],[5,6,7,8],[9,10,11,12]];
var CombinedData = NewData.map(x=>[...Data, ...x]);
console.log(CombinedData);
希望将单个数组添加到多维数组或数组数组中。
例如:
Data = [["A","B","C","D"]];
NewData =[[1,2,3,4],[5,6,7,8],[9,10,11,12]];
CombinedData = [];
for(i=0; i< NewData.length; i++){
CombinedData.push(...Data,NewData[i]);
}
但是上面的结果是这样的: "CombinedData =[["A","B","C","D"],[1,2,3,4],["A","B","C","D"], [5,6,7,8],["A","B","C","D"],[9,10,11,12],["A","B","C" "D"]...]"
我要找的是这个: "CombinedData =[["A","B","C","D",1,2,3,4],["A","B","C","D",5,6 ,7,8],["A","B","C","D",9,10,11,12],]"
这是在 ExcelScripts 或办公脚本中,我知道这是很新的但只是在寻找某种方向。
您需要展开两个数组并用括号括起来以创建一个新数组。
const Data = ["A","B","C","D"];
const NewData =[[1,2,3,4],[5,6,7,8],[9,10,11,12]];
var CombinedData = [];
for(i=0; i< NewData.length; i++){
CombinedData.push([...Data,...NewData[i]]);
}
console.log(CombinedData);
此外,您可以只使用 map 而不是 for 循环:
const Data = ["A","B","C","D"];
const NewData =[[1,2,3,4],[5,6,7,8],[9,10,11,12]];
var CombinedData = NewData.map(x=>[...Data, ...x]);
console.log(CombinedData);