[英]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表
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]
的访问就像它是一个数组而不是未定义的一样。
Use code guards to check and modify results accordingly.使用代码防护来相应地检查和修改结果。
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.