简体   繁体   中英

Google Script Map Function not returning value

I cobbled together some code a while back and its been working well but now something has changed and it's no longer working as expected. I have the line of code below which was working to return the location of the first time a matching value is found. I am pretty green at this but the best I can tell is that the map function iterates through every element of the array and returns a value based on the function called. In this case I believe the function is called e.

from the logs: lookup_array (first 3 elements only) I also removed the data other than the first element of each array as that is the relevant data.

[

[AC, , , , ,]
, [AC, , , , , ]
, [AC, , , , , ]

...]

newsheet_name = AC

The desired output would be 0 in this case since the newsheet_name matches the first record in this array. Unfortunately, I get a -1 result and can't figure out why.

var index = lookup_array.map(function(e) {return e[1]}).indexOf(newsheet_name);    

Issue:

You need to return e[0] instead of e[1] . Remember, JavaScript indexes start from 0 and you want to get the first element in the array.

Solution:

 const lookup_array = [ ["AC","","","",""], ["AC","","","",""], ["AC","","","",""], ]; const newsheet_name = "AC"; const index = lookup_array.map(e=>e[0]).indexOf(newsheet_name); console.log(index);

or if you don't want to use arrow functions , then use your solution with e[0] :

const index = lookup_array.map(function(e) {return e[0]}).indexOf(newsheet_name);  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM