0

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, "", "", ""]
];
3
  • Can you provide your expected output values? By the way, your input values are finalFabricData, finalDecorationData, finalHtData, finalOrderData, datasetIndexLength? Commented Nov 16, 2022 at 5:02
  • I've just edited the question to both try to explain it more clearly and to display an expected result. Thanks, @Tanaike! Commented Nov 16, 2022 at 5:17
  • Thank you for replying and adding your expected values. From your expected values, I proposed a sample script as an answer. Could you please confirm it? If I misunderstood your question, I apologize. Commented Nov 16, 2022 at 5:27

1 Answer 1

1

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.

References:

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer! One question: The length of these datasets may vary. finalDecorationData may have 5 rows and that would become the base dataset that others would get concatenated to. This is why I push the variable name and its length into dataSizes so that I can grab the one with max rows. Now grabbing the largest dataset dynamically, which is then a string in the first column in dataSizes is my challenge. Thank you!
@onit Thank you for replying. I have to apologize for my poor English skill. Unfortunately, I couldn't understand your reply. But, I would like to support you. So, can I ask you about the detail of your reply? By this, I would like to try to understand your reply.
A piece missing requires me to eval()and your solution does build the end result as expected. Thank you!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.