Home > Mobile >  Why is this map function not returning one of the number, when it gets 1 to subtract from using GAS?
Why is this map function not returning one of the number, when it gets 1 to subtract from using GAS?

Time:05-02

I'm getting the numbers in a column and subtracting 1 from it, when the value it not empty:

function myFunction(){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const colSettingSheet = ss.getSheetByName('Settings');
let colSettings = colSettingSheet.getRange('D4:E').getValues();

let indexes = colSettings.map(e => e[1] > 0 ? e[1] - 1 : e[1]).filter(e => e != '');
}

The original values:

[[6,7,8,1,,,,14,31,17,30,15,16,34]]

This is what it's returning:

[5,6,7,13,30,16,29,14,15,33]

...and it's missing a 0 between 7 and 13 and I can't find out why.

Appreciate your input!

CodePudding user response:

You need filter out undefined, not empty string.

And as Alexey Zelenin suggested, filter and map should be swapped

let colSettings = [6,7,8,1,,,,14,31,17,30,15,16,34].map(a => [0,a]);
let indexes = colSettings.filter(e => e !== undefined).map(e => e[1] > 0 ? e[1] - 1 : e[1]);
console.log(indexes);

  • Related