Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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]].

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading