What is the method to retrieve the index or row number using the map function in Google Sheets?

Currently, I am attempting to retrieve the row number where the row meets certain criteria. However, I seem to be encountering an issue:

  1. Instead of getting the desired result, I am obtaining an array like this: [,,2]
  2. Although I have attempted using filter() at the end, it has proven to be ineffective. Additionally, I am uncertain about its efficiency since I am already utilizing map to iterate through the data in order to obtain 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 Outcome: 1 //2nd row

This is my attempt at modifying it based on this response

Answer №1

Finding the correct row number

function getRowNumber() {
  const searchArray = ["ABC", 25];
  const dataArray = [["CBA", 14],
  ["ABC", 25],
  ["DRT", 34]];
  const spreadsheet = SpreadsheetApp.getActive();
  const sheet = spreadsheet.getSheetByName('sheet name');
  const range = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
  const dataValues = range.getValues();
  let result = dataValues.map((row, index) => { if (row[0] == searchArray[0] && row[1] == searchArray[1]) { return index + 1; } });
}

Answer №2

It's recommended to replace Array.prototype.map with Array.prototype.findIndex

arr = ["ABC", 25];

searchArr = [["CBA", 14],
      ["ABC", 25],
      ["DRT", 34]]
      
const index = searchArr.findIndex((item,index) => item[0] == arr[0] && item[1] == arr[1]);
console.log(index);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I retrieve my array state in a different router page using VUEJS and VUEX?

I have created a page with two routes - one for the home page and another for the configuration where users can decide what content should be displayed on that container. In the configuration panel, I was able to retrieve input values and stored them in my ...

I encountered a problem while trying to incorporate the solution that prompts another button click event

Interesting topic: JQuery / JavaScript - triggering button click event from another button <input type="submit" name="savebutton" id="uniqueOne" /> <input type="submit" name="savebutton" id="uniqueTwo" /> I have implemented a feature on my si ...

The significance of the "? :" operator in JavaScript

These are the lines of code I am currently working with: toggleMore : function( $item, show ) { ( show ) ? $item.find('a.ca-more').show() : $item.find('a.ca-more').hide(); }, Could someone help me understand what this code does? ...

Center Align Images in HTML List Items

Hey there! I'm currently diving into the world of web development with responsive design, but I'm still a bit of a newbie. So please bear with me as I try to explain my issue in detail. If you need more information, just let me know! My current ...

Preventing default behaviors in VueJS textarea components

I'm attempting to incorporate a slack-like feature that sends a message only when the exact Enter key is pressed (without holding down the Shift key). Looking at this Vue template <textarea type="text" v-model="message" @keyup.enter.exact="sendMe ...

How to pass GetServerSideProps into Page component props in Next.js with custom _app.js

Having issues integrating GetServerSideProps into my Next.js app. Despite the network call successfully fetching data and generating the pageName.json, the response data is not being injected into the page props as expected. Below is a snippet of my page ...

How can we make sure the selected tab opens in jQuery tabbed content?

I'm trying to make my tabbed content open on a specific tab by changing the URL. I remember seeing something like this before but can't seem to find it! Here's the fiddle I created: http://jsfiddle.net/sW966/ Currently, the default tab is ...

Utilizing Jquery Ajax to send a post request containing input values through Tampermonkey

I encountered an issue with my code - even though the logs in console show that it is working, I am unable to send the values to my server using AJAX post. jQ(document).on("keyup", "form input", function () { var value = jQ(this).val(); ...

I am looking for a specific task to be carried out, without any need for returning a value or any additional items

I am trying to remove an element from the canvas using Selenium. The command I am currently using is "window.app.design.internalLayer().find('.deletebutton').fire('click')". Despite my efforts, this command does not seem to be working a ...

The information from the form is not appearing in the req.body

Utilizing the mean.js framework, I have the bodyParser middleware configured as shown below: app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(methodOverride()); Additionally, I am using formidable to upload imag ...

Rendering JSON data in a dropdown menu

When I select an option, all the data is being combined into one row. I want the data to fill each row based on the select option. Any suggestions on what code I should modify or add? Is it related to the loop or should I create a dynamic id for the selec ...

Exploring the dimensions of a Numpy array

Is there a way to determine the dimensions of an array like this one, which is 2x2: matrix = np.array([[1, 2], [3, 4]]) ...

I have noticed that the baseline of a Span element has shifted after updating my Chrome browser to a version that begins with

Once I updated to chrome Version 108.0.5359.94 (Official Build) (64-bit) from 107.0.5304.87 (Official Build) (64-bit), the behavior of the span element changed drastically. It shifted its baseline when multiple spans were stacked on top of each other. Exp ...

The code functions properly when tested on a local server, but doesn't produce any results when

Currently, I am working on customizing a premade website and have been tasked with making a modification. The goal is to implement a confirmation box that appears when a button to delete a meeting task is clicked, rather than immediately deleting it. Afte ...

Obtaining a value from an array in a controller

There is a function within the controller that includes an array. function retrieve_articles(){ $data['retrieved']=$this->article_model->get_article(); $this->load->view('initial',$data);} I am looking to extract the secti ...

Vue JS - Unable to locate module (datepicker)

Today marks my first time delving into the world of vue.js, and as expected, I've encountered an error that has me stumped. I recently incorporated the v-md-date-range-picker module into my project: (. The setup instructions provided me with the f ...

Load grid data only when the tab is clicked in ExtJS

Our app features a dynamic grid loaded with multiple tabs, each containing one or more grids. The issue currently is that when the application loads, it automatically calls all the URLs instead of waiting for the user to click on a tab. We want to optimi ...

spring fails to run javascript

In my project, I am utilizing Spring MVC 3 framework. Below is the snippet of my AddressController: @Controller public class AddressController { private static Logger logger = Logger.getLogger(AddressController.class); @RequestMapping(value="/ad ...

Unable to establish a connection with the websocket server

I set up a websocket server on my host at <www.example.com>, but for some reason, the client is unable to connect to it. Here is the code : Server.js : const ws = new require('ws'); const wss = new ws.Server({noServer: true}); const htt ...

The Discord.js .cleanContent attribute does not show up when using Object.keys() and cannot be logged

My Discord.js Message object should contain a property called .cleanContent, according to the documentation, and it should be a string. console.log(message.cleanContent) works correctly, but console.log(message) does not display the cleanContent propert ...