any way to use app script javascript to filter with an additional parameter?

I have a Google Sheets spreadsheet with a bunch of stock data and the ticks/symbols on it. The problem is that I need to filter for a particular symbol (and do it about 90 times), and don't wish to have 90 different filter functions for each tick/symbol, but I see no way to create a filter function and pass in something other than an array to it. Right now I have:

let sheet = combinedSheet.getRange("A2:G3945").getValues();
let sheetFiltered = sheet.filter(filterByTickApple);

let filterByTickApple = function(item) {
     if (item[2] === "AAPL") {return true} else {return false};
}

Basically, you can see that I'd have to write 90 separate functions. What I'd like to do would be something that looks like this:

  let sheetFiltered = sheet.filter(filterByTick,"AAPL");

  let filterByTick = function(item, tick) {
      if (item[2] === tick) {return true} else {return false};
  }

but I know this is not possible as written (I've tried variations, but no joy). Someone else posted that supposedly, you can put other arguments in, but as per the docs, that's not possible (and I've tried their code - it didn't work)

Any idea for a workaround/hack?


I believe your goal is as follows.

  • You want to retrieve the rows including AAPL and other values in the column "C".

Although I'm not sure whether I could correctly understand your goal, if my understanding is correct, how about the following modification?

Modified script:

var values = ["AAPL","###",,,]; // Please set the values you want to check.
let sheet = combinedSheet.getRange("A2:G3945").getValues();
let sheetFiltered = sheet.filter(e => values.includes(e[2]));

References:

  • filter()
  • includes()