Home > Enterprise >  Is there a way to pass and additional parameter through a filter?
Is there a way to pass and additional parameter through a filter?

Time:01-29

I am filtering a range based on a condition. The condition is not always the same so I wanted to add is as a parameter to my filter function. My error comes when I call the custom filter function while passing through my additional it no longer looks at each individual item in the array and instead just the whole array.

function myFunction() {
  var arr = [[1, 3, 1], [2, 3, 1], [1, 3, 2]];
  myInt = 1;

   return arr.getValues().filter(isMyInt(arr.getValues(), myInt));
}

function isMyInt(arr, myInt) {
  return arr[0] == myInt;
}

In this example my desired result would be [[1, 3, 1], [1, 3, 2]].

CodePudding user response:

Try this:

function myFunction() {
  Logger.log([[1, 3, 1], [2, 3, 1], [1, 3, 2]].filter(r => r[0] == 1));
}


Execution log
2:23:11 PM  Notice  Execution started
2:23:12 PM  Info    [[1.0, 3.0, 1.0], [1.0, 3.0, 2.0]]
2:23:13 PM  Notice  Execution completed
  •  Tags:  
  • Related