简体   繁体   English

尝试按索引访问 Map 的数组值时出现未定义错误

[英]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. array1[0] 是字符串类型,与我的 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.为了使这种重现性生成一个谷歌表,其中第一个表名为 values1,第二个表名为 values2。 You have to run this on Google Apps Script.您必须在 Google Apps 脚本上运行它。

Please see the images for the sample values:请参阅图像以获取示例值:

values1 sheet值1张

values2 sheet值2表

Issue:问题:

tagMap doesn't have array1[0] key => returns undefined => errors, when accessing property 0 of undefined tagMap没有array1[0]键 => 访问undefined的属性0时返回undefined => 错误

TypeError: Cannot read property '0' of undefined TypeError:无法读取未定义的属性“0”

This is because, property [0] is accessed as if it is a array and not undefined.这是因为,属性[0]的访问就像它是一个数组而不是未定义的一样。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将“未定义”索引映射到数组 - Map “undefined” index to Array 类型错误:无法读取未定义的属性(读取“地图”)。 当我尝试通过一系列加密货币 map 时抛出错误 - TypeError: Cannot read properties of undefined (reading 'map'). Error being thrown when I am trying to map over an array of cryptocurrencies 尝试访问 React state 变量中的嵌套数组时如何解决未定义错误? - How to solve undefined error when trying to access a nested array in a React state variable? Array.map 索引未定义? - Array.map index is undefined? 尝试访问 Node.js 中解析的 JSON 值时出现 undefined:1 错误 - undefined:1 error when trying to access parsed JSON value in Node.js 尝试在PHP中回显输入文本时出现未定义的索引错误? - Undefined index error when trying to echo input text in PHP? jQuery数组无法在php脚本中访问错误:未定义索引 - Jquery array cannot access in php script Error: Undefined Index 当我尝试通过索引访问数组元素时,它给了我未定义的 - when i try to access array element by index it gives me undefined 尝试访问数组对象中的属性时未定义 - Getting undefined when trying to access properties in array object 尝试访问数组中的对象时,grep返回未定义 - grep returning undefined when trying to access object in array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM