简体   繁体   English

下面的JavaScript映射有什么问题?

[英]What is wrong with the JavaScript Mapping Below?

const myAssetMap = {
  General: {
    name: 'Common',
    bkgHex: '#eee',
    fontHex: '#aaa',
  },
 ...

const getAsset = (name, type) => {
  return myAssetMap.name.type;
}

console.log( getAsset('General', 'name') );

This is erring with: Uncaught TypeError: Cannot read property 'type' of undefined 这是错误的: Uncaught TypeError: Cannot read property 'type' of undefined

You are trying to access the keys name and type , not the keys stored in the variables. 您正在尝试访问键的nametype ,而不是访问存储在变量中的键。 Use bracket notation instead: 请改用方括号表示法:

return myAssetMap[name][type];

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM