Although I've found similar questions about the IndexOf function, none of the answers have resolved my specific issue.
In my case, I have a large 2D array containing names and ID codes from a spreadsheet, which I read into an array named rIdr
in my apps script.
To search for a name in the first array and retrieve the corresponding value from the second array, I create two one-dimensional arrays using the IndexOf function:
var keys=[]; var vals=[];
//build key-val lookup arrays
for (var i = 0; i < rIdr.values.length; i++){
var k = rIdr.values[i][0].toString()
keys[i]=k
var v = rIdr.values[i][1].toString()
vals[i]=k
}
My search involves names obtained from a JSON object, and iterating over these names to find matches in the key
and val
arrays:
jsonobj.data.forEach(function(value) {
var idx = keys.indexOf(value.first_names_txt + " " + value.last_name_txt)
var id = -1;
if (idx > -1){id = vals[idx]}
Logger.log(value.first_names_txt + " " + value.last_name_txt + " " + id)
});
Despite verifying the data types and contents of the arrays, the IndexOf function consistently returns -1, even for names that should match. For example:
var test
test = keys.indexOf("Joe Bloggs")
I'm struggling to understand why IndexOf is not working in this scenario and I would appreciate any insights or alternative solutions that avoid passing or declaring large arrays as global variables.
Thank you for your assistance.