Home > database >  Returning instance numbers, not values, of a dataset that passes a filter function
Returning instance numbers, not values, of a dataset that passes a filter function

Time:04-16

How do you write a filter function in GAS that, rather than returning values of a dataset that pass the condition, returns the instance numbers of the data that passes the condition?

For example, let's say our condition is divisibility by 10.

(value % 10 = 0)

And our dataset is

[1,5,10,20,7,40]

Items # 0, 1, and 4 fail the condition; items # 2, 3, and 5 pass. Desired result:

[2,3,5]

CodePudding user response:

function lfunko() {
  Logger.log([1,5,10,20,7,40].map((e,i) => (e % 10 == 0)? i: '').filter(e => e !== '').join(','))
}

Execution log
4:42:39 PM  Notice  Execution started
4:42:40 PM  Info    2,3,5
4:42:40 PM  Notice  Execution completed

enter image description here

References:

  • Related