【发布时间】:2019-09-03 10:48:17
【问题描述】:
假设我有一个这样的 JSON 文件:
{"ABCD":{
"1_99":{"type": 3, "serverPath": "http://some.website.com"}
},
"EFGH":{
"1_00":{"type": 2, "serverPath": "http://another.meme.website/"}
"1_01":{"type": 2, "serverPath": "http://yet.another.website.com/memes"}
},
etc..
}
我想引用该文件中的元素之一:
let appList = require('./config/appList.json');
...
var uri = appList.ABCD.1_99.serverPath;
其中元素名称“ABCD”和“1_99”来自另一个对象,并且并不总是相同,比如:
var uri = appList. [httpPOSTRequest.app_id] . [httpPOSTRequest.app_ver] . serverPath;
请问有什么办法可以做到的。
【问题讨论】:
-
离你不远了。尝试像
var uri = gameList[ key1 ][ key2 ].serverPath;这样的格式。 -
你好大卫,阅读更多关于属性访问器的信息developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
-
更好的是,在取消对下一个引用之前,通过验证检查每个引用重新编写。所以检查 (gameList[key1] != undefined) 然后 (gameList[key1][key2] != undefined) 最后完整参考。