0

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

13
  • provide minimal reproducible example. Show sample array1 ,myValues data and myMap creation code. Make a simple code with sample values, which we can load in the script editor to reproduce the same error. Commented Apr 6, 2020 at 20:55
  • @TheMaster Added examples of values. Commented Apr 6, 2020 at 21:16
  • no repro even after fixing all syntax errors. You can make a working snippet here itself. or test the code in your script editor. see repro of minimal reproducible example Commented Apr 6, 2020 at 21:30
  • Possible guesses: You're rewriting/reassigning array1 or myMap before the last line. Without repro, don't think anyone can help you though Commented Apr 6, 2020 at 21:35
  • @TheMaster Added more code and instruction to generate sheet. Hope its understandable. Thanks again. Commented Apr 6, 2020 at 22:30

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.