3

I've this 2D array of data (let's call the variable arr) that represents a table with various fields:

     [1]   [2]   [3]   [4]   
[1],Fruit,Apple,Red,10  
[2],Fruit,Apple,Green,20  
[3],Berry,Strawberries,Red,5  
[4],Tuber,Potato,Yellow,2  

In this case I need to filter the arr variable by column 3 = Red (I don't want to search Red in all the table, just in column 3) obtaining this:

    [1]   [2]   [3]   [4]   
[1],Fruit,Apple,Red,10  
[2],Berry,Strawberries,Red,5 

How is it possible to apply the .filter function to a 2D array in order to filter for a single field/column?

pnuts
  • 58,317
  • 11
  • 87
  • 139
d4rk5h4rk
  • 67
  • 2
  • 2
  • 6

1 Answers1

5

ECMAScript 6

let filtered = arr.filter(dataRow => dataRow[2] === 'Red');

As noted by @ozeebee, ES6 is currently not supported in Google App Scripts, so you should try the following:

ECMAScript 5

var filtered = arr.filter(function (dataRow) {
  return dataRow[2] === 'Red';
});

In the comments, “classic way” refers to the ES5 method.

Explanation

.filter function takes a single parameter which is a callback to a function that returns true if array entry should remain or false if it should be removed, that’s the filtering. In this case, we should check whether third column of table row equals to Red. The code: return dataRow[2] === 'Red' is equal to:

if (dataRow[2] === 'Red') {
  return true;
} else {
  return false;
}

Because the result of comparison is a boolean.

See also

gsc
  • 1,559
  • 1
  • 13
  • 17
  • Got it, now it works. i was not knowing that for javascript the column of a table are considered dataRow – d4rk5h4rk Nov 28 '16 at 17:25
  • It's not a JavaScript thing. I chose such name because it's relatively self-descriptive (row of data table). – gsc Nov 28 '16 at 17:32
  • For array `[[null, null, new Date()], [null, null, new Date('2015-01-01')]]` it works… Have you passed correct index (2)? – gsc Dec 01 '16 at 12:12
  • i've written this code: var filtered = arr.filter(function (dataRow) { return dataRow[2].getFullYear() === 2016; }); but the system gives the error "impossibile to find function .getFullYear() in the object – d4rk5h4rk Dec 01 '16 at 13:43
  • You've said that you can read correct year using `dataRow[2][2]`, but where's it? If inside `arr.filter(function (dataRow) {…})`, then is `arr` a three-dimensional array (`[[[]]]`)? If not, have you declared `dataRow` earlier? Does it work if you set `arr` to `[[null, null, new Date()], [null, null, new Date('2015-01-01')]]`? – gsc Dec 01 '16 at 14:40
  • Can you log `dataRow[2]` and `typeof dataRow[2]` (maybe `dataRow[2] instanceof Date` if Logger doesn't tell it otherwise)? – gsc Dec 01 '16 at 17:35
  • If I Logger.log(dataRow[2]); the system tells me that dataRow is undefined (it tells the same for just dataRow, dataRow[1] etc). IDK if it's useful, but if i typeof arr[2][2] i get "object" – d4rk5h4rk Dec 02 '16 at 16:35
  • if i instanceof Data arr[2][2] i get "true" – d4rk5h4rk Dec 02 '16 at 16:44
  • Have you added that Logger inside the callback function? Eg. `arr.filter(function (dataRow) {Logger.log(dataRow);});`. If so, it shouldn't be `undefined`. Are Google App Scripts run in the browser or on a remote server? – gsc Dec 02 '16 at 19:30
  • I wasn't logging inside the callback function... So, now, typeof dataRow[2] gives me a column of "object", and dataRow[2] instanceof Date gives me a column of "true" – d4rk5h4rk Dec 02 '16 at 22:31
  • 3
    Note that the first form, "Modern way", does not work with GAS scripts because it doesn't support (yet) the javascript ES6 syntax. GAS scripts are compiled to java code and run on Google servers. – ozeebee Mar 02 '17 at 15:43