Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This works for me

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet1');
  let s = sh.getRange('D4:E' + sh.getLastRow()).getValues();
  Logger.log(s.map(e => e[1] > 0 ? e[1] - 1 : e[1]));
}

You do not require the filter if you simply eliminate the nulls in the first place with lastRow

about 4 years ago · Juan Pablo Isaza Report

0

You need filter out undefined, not empty string.

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

let colSettings = [6,0,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);
And since filter automatically removes empty items, you can use filter(Boolean) instead.

[EDIT] You don't really need loop twice through the array with filter and map, you can use reduce and filter it in one shot:

let colSettings = [6,0,7,8,1,,,,14,31,17,30,15,16,34].map(a => [0,a]);
let indexes = colSettings.reduce((r,a) => ((r[r.length] = a[1] - (a[1] && 1)), r), []);
console.log(indexes);

P.S. Your original code suggests that the original array format is different then you've posted, it's more like: [[0,6],[0,7],[0,8],[0,1],,,,[0,14],[0,31],[0,17],[0,30],[0,15],[0,16],[0,34]]

If the array is actual in format you've posted [[6,7,8,1,,,,14,31,17,30,15,16,34]] than the code for that would be:

let colSettings = [[6,0,7,8,1,,,,14,31,17,30,15,16,34]];
let indexes = colSettings.map(ar => ar.reduce((r,a) => ((r[r.length] = a - (a && 1)), r), []));
console.log(indexes);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!