Google Script – How to find the index of a nested Array in the search Array?

I want to find the index of the NestedArray (userdata) inside the SearchArray (data). I already have the existing code inserted below.

var data = [["John",25],["James",19],["Liam",27],["George",22]]
var user = "Liam";
var userdata = [];

const userfound = data.some(function(row){
    if(row[0] == user){ 
      userdata = row; 
      return true;
    }
    else {return false;}
});

console.log(userfound);
console.log(data);
console.log(userdata);

The code above gives the corresponding log:

true
Array [Array ["John", 25], Array ["James", 19], Array ["Liam", 27], Array ["George", 22]]
Array ["Liam", 27]

normally I would try to find an index of an item in an Array like so:

const index = data.findIndex( x => userdata.includes(x)); 

However this doesn't work for a 2d Array. Does anyone have a clue?



Read more here: https://stackoverflow.com/questions/67939522/google-script-how-to-find-the-index-of-a-nested-array-in-the-search-array

Content Attribution

This content was originally published by SmexyKoen at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: