简体   繁体   中英

Undefined error when trying to access Array value of Map by index

My code is the following:

let myMap = new Map(
                     [["MMP", ["H1", "-"]], ["TTP", ["H1", "*"]]
);

var array1 = ["MMP", 1, 2]; // array1[0] is the key I want to find in my map

console.log(myMap.get(array1[0]); // Works: gets the values for the given key => [value1,value2]

console.log(myMap.get('example key')[0]); // Works: gets the first element of the array value

console.log(myMap.get(array1[0])[0]); // TypeError: Cannot read property '0' of undefined
                                     // array1[0] is the same as 'example key'

I cannot see what I'm doing wrong here. array1[0] is of type string, the same as the key in my map.

Your help is very much appreciated!

EDIT: Adding more complete code:

var tagSheet = SpreadsheetApp.getActive().getSheetByName('values1');
var docSheet = SpreadsheetApp.getActive().getSheetByName('values2');
var myTagRange = tagSheet.getDataRange();
var myDocRange = docSheet.getDataRange();
var tagValues = myTagRange.getValues();
var docValues = myDocRange.getValues();

function compare(){

  var array1 = []; 

  var columnToCheck = docSheet.getRange("A:A").getValues();

  // Get the last row based on the data range of a single column.
  var docLastColumn = getLastRowSpecial(columnToCheck);

  var tagMap = new Map(getTagMap());


  //Loop over values2 sheet
  for(var n=3;n<docLastColumn;n++){


    array1 = docValues[n][0].split("*");

   if( tagMap.get(array1[0])[0] === 't1'){ //Error here
      Logger.log('passed');
       }

  }

}

function getTagMap() {

  //Select the column we will check for the first blank cell
  var columnToCheck = tagSheet.getRange("A:A").getValues();

  // Get the last row based on the data range of a single column.
  var lastRow = getLastRowSpecial(columnToCheck);
  let myMap = new Map();

   for(var n=1;n<lastRow;n++){ 
     myMap.set(tagValues[n][0].trim(), [tagValues[n][1].trim(),tagValues[n][2].trim()]);
   }

  return myMap;

};


function getLastRowSpecial(range){
  var rowNum = 0;
  var blank = false;
  for(var row = 0; row < range.length; row++){

    if(range[row][0] === "" && !blank){
      rowNum = row;
      blank = true;

    }else if(range[row][0] !== ""){
      blank = false;
    }
  }
  return rowNum;
}

To make this reproducible generate a Google Sheet with the first sheet named values1 and second sheet named values2. You have to run this on Google Apps Script.

Please see the images for the sample values:

values1 sheet

values2 sheet

Issue:

tagMap doesn't have array1[0] key => returns undefined => errors, when accessing property 0 of undefined

TypeError: Cannot read property '0' of undefined

This is because, property [0] is accessed as if it is a array and not undefined.

Solution:

Use code guards to check and modify results accordingly.

Snippet:

const thisMapValue = tagMap.get(array1[0]);
if(thisMapValue && thisMapValue[0] === 't1'){ //ensures  `thisMapValue` is not undefined before accessing `[0]` property

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