Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
Improving efficiency of adding a column to an array in a loop?

So I wrote some code that goes to a book's ID based on a "dictionary" and takes the data from all the sheets in that book after the second sheet. That data is then aggregated into a single array. My question centers around the idea of improving the for loop in the if statement. That is adding the "supplier name" to the front of the subarray that represents a row in the sheet/data. I was told that this is inefficient as the code has to go through each subarray and add a value.

Is there a more efficient way of doing this? I did it this way because I am copying the values of a range which are stored as an array of arrays. So to add data, I have to reaccess the subarrays. Is it possible to add the new data (supplier name) at the same time that the values are being copied? would this be more efficient? It was recommended to use an arrow function, however, I am not familiar with their usage.

function aggregate() {
   var combinedData = []
   var idArray = {
   "suppliername":"id",
   };
for (var supplierName in idArray){
   var sBook = SpreadsheetApp.openById(idArray[supplierName]);
   var sheets = sBook.getSheets();

   for (var index = 2; index <sheets.length; index++){
      var sheet = sheets[index];
      var dataLength = sheet.getRange("E5:E").getValues().filter(String).length;

      if(dataLength != 0){
         var dataRange = sheet.getRange(5,2,dataLength,14);
         var dataValues = dataRange.getValues();
      
         for (row in dataValues) {
           dataValues[row].unshift(supplierName);
         };
         combinedData = combinedData.concat(dataValues);
      };
   };
};

var dataLength = combinedData.length;
const dSheet = SpreadsheetApp.openById("id").getSheets()[0];
dSheet.getRange(2,1,dSheet.getMaxRows(),dSheet.getMaxColumns()).clearContent();
var dRange = dSheet.getRange(2,1,dataLength,15);
dRange.setValues(combinedData);
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your script, how about the following modification?

From:

for (row in dataValues) {
  dataValues[row].unshift(supplierName);
};
combinedData = combinedData.concat(dataValues);

To:

combinedData = combinedData.concat(dataValues.map(e => [supplierName].concat(e)));

or

combinedData = [...combinedData, ...dataValues.map(e => [supplierName, ...e])];

References:

  • map()
  • Spread syntax
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!