In order to find the largest dataset among others and concatenate them, I'm sorting them by their lengths.
Now, I'd like to concatenate them, but I'd need to turn these strings into the variable name, so that I can iterate through each of these datasets, concatenating them.
I've seen that window[]
is used in Javascript, but what about in GAS?
function concatenateData() {
let dataSizes = [];
//Separated datasets
let finalFabricData = [
["A", "C", 3],
["S", "R", 4],
["C", "O", 3]
];
dataSizes.push('finalFabricData', finalFabricData.length);
let finalDecorationData = [
["T", "D", 3],
["F", "F", 4],
["G", "O", 3]
];;
dataSizes.push('finalDecorationData', finalDecorationData.length)
let finalHtData = [
["A", "C", 3],
["S", "R", 4],
["K", "O", 3]
];
dataSizes.push('finalHtData', finalHtData.length);
let finalOrderData = [
["Z", "C", 3]
];
dataSizes.push('finalOrderData', finalOrderData.length);
//Attempt to grab the variable names, but they come as strings
let data1 = dataSizes[0][0];
let data2 = dataSizes[1][0];
let data3 = dataSizes[2][0];
let data4 = dataSizes[3][0];
//Attempt to concatenate them, but data1 is not iterable
let finalDataset = [];
data1.forEach(function(row, i) {
row = row.concat(data2[i], data3[i], data4[i]);
finalDataset.push(row)
});
}
Exected Result
let res = [
["A", "C", 3, "T", "D", 3, "A", "C", 3, "Z", "C", 3],
["S", "R", 4, "F", "F", 4, "S", "R", 4, "", "", ""],
["C", "O", 3, "G", "O", 3, "K", "O", 3, "", "", ""]
];
Thanks
CodePudding user response:
In your situation, how about the following sample script?
Sample script:
// These are from your script.
let finalFabricData = [
["A", "C", 3],
["S", "R", 4],
["C", "O", 3]
];
let finalDecorationData = [
["T", "D", 3],
["F", "F", 4],
["G", "O", 3]
];;
let finalHtData = [
["A", "C", 3],
["S", "R", 4],
["K", "O", 3]
];
let finalOrderData = [
["Z", "C", 3]
];
// I modified the below script.
const arrays = [finalFabricData, finalDecorationData, finalHtData, finalOrderData];
const res = arrays[0].map((_, c) => arrays.flatMap(r => r[c] || Array(3).fill("")));
console.log(res)
- When this script is run, your expected values are retrieved.