Comprehension of GAS

Here, I would like to introduce a comprehension of GAS.

Input :

var data = [[[0], [1], [2], [3]], [[4], [5], [6], [7]]];

Output :

[[0.0, 2.0], [0.0, 2.0]]

Pattern 1

var a = [];
for (var i=0; i<data.length; i++) {
  var temp = [];
  for (var j=0; j<data[i].length; j++) {
    if (data[i][j][0] % 2 == 0) temp.push(j);
  }
  a.push(temp);
}
Logger.log(a)

Pattern 2

var b = [];
data.forEach(function(e1){
  var temp = [];
  e1.forEach(function(e2, i2){
    if (e2[0] % 2 == 0) temp.push(parseInt(i2, 10));
  });
  b.push(temp);
});
Logger.log(b)

Pattern 3

var c = [[parseInt(i, 10) for (i in e) if (e[i][0] % 2 == 0)] for each (e in data)];
Logger.log(c)

GAS can use JavaScript 1.7. So it can write as above.

  • In the case of [i for (i in array)], i is index. But it’s string.
  • In the case of [i for each (i in array)], i is element.

 Share!