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

How to get the index/row number using map in Google Sheets?

I’m trying to get the row number, as that row meets a couple of criteria, but:

  1. I’m getting an array like this: [,,2]
  2. I’ve tried using filter() at the end, but it doesn’t work and I’m not sure how efficient that’d be, if I’m already using map going through the data to get the row:
ar = ["ABC", 25];

vs = [["CBA", 14],
      ["ABC", 25],
      ["DRT", 34]]

function f101() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('sheet name');
  const rg=sh.getRange(1,1,sh.getLastRow(),sh.getLastRow());
  const vs=rg.getValues();
  let holder=vs.map((r,i)=>{if(r[0]==ar[0] && r[1] == ar[1]){return i;}});
}

Expected Result:
1 //2nd row

This is me trying to adapt it from this answer

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 :

Instead of Array.prototype.map use Array.prototype.findIndex

ar = ["ABC", 25];

vs = [["CBA", 14],
      ["ABC", 25],
      ["DRT", 34]]
      
const h = vs.findIndex((r,i) => r[0] == ar[0] && r[1] == ar[1]);
console.log(h);
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