How to properly iterate through this two dimensional javascript array? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to properly iterate through this two dimensional javascript array? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a sheet like this:

enter image description here

And I have this function:

    function getRangeAsArrays(sheet) {
  var dataRange = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn());
  var data = dataRange.getValues();
  var array = [];  
  for (var r=0; r<sheet.getLastColumn(); r++) {
  for (i in data) {
    var row = data[i];
    array.push(row);
       }
    }
  return array;
}

Which I use to build a listboxthis way:

   var recipientArray = getRangeAsArrays(activeSheet);
  var item3Panel = app.createHorizontalPanel();
  item3Panel.add(app.createLabel("recipient"));
  var listBox = app.createListBox().setName('item3');  
  for(var i = 0; i < (recipientArray.length); i++){
  Logger.log("recipientArray[i][2] = " + recipientArray[i][3]);
  Logger.log(" i = " + i);
      listBox.addItem(recipientArray[i][4]);
    }
  item3Panel.add(listBox);

But when I iterate over the array length (4 rows), I got this (unexpected to me) result and the logs shows i variable goes until 14:

enter image description here

Since recipientArray.length should give me the first dimension of the 2 dimensional array and recipientArray[i].length the second dimension, and since I want the first dimension (number of rows) row to fix that? What is going wrong here?

Answer

Even if I’m still unsure that I understood what you need (I guess I’m a bit tired or I become stupid… go figure…:), I wonder why you try using separate functions since the value returned by range.getValues() is already an array… A 2D array but still an array.

If you want to create one listBox per row and add the following cells as items then a double loop like this will do the job. (tell me if I’m completely off the subject, thx).

I wrote an example code with the main structure and comments to explain where things go.

function test() {
  var dataRange = SpreadsheetApp.getActiveSheet().getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn());
  var data = dataRange.getValues();
  Logger.log(data)
  var array = [];  // this is useless
  for (var r=0; r<data.length; r++) {

    // create listBox widget here
    //var listBox = app.createListBox().setName('listBox'+r);   
    for (i in data[0]) {
      var cell = data[r][i];

      //add items to listBox here
      // listBox.addItem(cell);
      array.push(cell);// this is useless
    }
  }
  Logger.log(array);//useless
  return array;//useless
}
We are here to answer your question about How to properly iterate through this two dimensional javascript array? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji