0

I'm just learning loops and can't get this part of the below script - what does values [i][0] mean here? (code found in this answer, described as a stopping condition). Is it the end and start of the array?

function getFirstEmptyRowByColumnArray() { 
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var values = ss.getRange('A:A').getValues(); 
  var i = 0; 

  while (values[i] && values[i][0] !== "") { 
    i++; 
  } 

  return i + 1;
}
Viktor Zhurbin
  • 134
  • 1
  • 11

2 Answers2

3

values is a more dimensional array. So the first index points to an array and the second index gives the element of that array. You can also imagine it as a matrix with rows and columns. The first index is the row and the second is the column.

If you want to know more about multi dimensional arrays in Javascript you can check out the answers to this question: How can I create a two dimensional array in JavaScript?

Community
  • 1
  • 1
Martin Cup
  • 2,399
  • 1
  • 21
  • 32
2

Every array index operator (square brackets, aka computed member access aka bracket notation) accesses a single array element. So if you see them in series, you are dealing with an array of arrays (or it will result in an error). For example:

var arr = [
   [1, 2, 3], // inner array
   [4, 5, 6], // inner array
];
arr[0]         // [1, 2, 3]
arr[0][1]      // 2
arr[0][1][2]   // error: 2 is not an arry
Ethan Brown
  • 26,892
  • 4
  • 80
  • 92