0

Can someone help with syntax on returning any specific column of an array in Google Apps Script / JavaScript.

Array

01 02 03 04 05
06 07 08 09 10
11 12 13 14 15

2nd row of array...

Browser.msgBox(values[1]);
06 07 08 09 10

what's the syntax for returning values of 2nd column?

Are there simpler way without the for loop path, reason being I have a 7k row by 28 column array and trying to mak it more efficient via better call methods.

Rubén
  • 34,714
  • 9
  • 70
  • 166
user1488934
  • 247
  • 2
  • 5
  • 13

2 Answers2

2
Browser.msgBox(values[x][y]);

Where x is the row, y is the column.

I don't know why you are getting the second row via 2 though. Arrays "zero-indexed". They start with 0. You should be getting the third row with 2.

Joseph
  • 117,725
  • 30
  • 181
  • 234
2

You can't get the values of second column directly, you will have to aggregate them manually.

Ex:

function getColumn(array, column){
    var result = [];
    for(var i = 0; i< array.length; i++){
        result.push(array[i][column]);
    }
    return result;
}

Then

var column2 = getColumn(myArray, 1)
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • Are there any simplier way than having to generate a function just for this? – user1488934 May 17 '13 at 06:35
  • @user1488934 AFAIK no there is not. Another question which dealt with same requirement http://stackoverflow.com/questions/7848004/get-column-from-a-two-dimensional-array – Arun P Johny May 17 '13 at 06:37