简体   繁体   English

Javascript:按名称访问对象的成员

[英]Javascript: access to an object's member by name

I have an object called themesData : 我有一个名为themesData的对象:

var themesData = {}
themesData.a = { key: "value" }; 
themesData.b = { key: "another value"};

...and I want to access one of the members by its name. ...并且我想通过其名称访问成员之一。 I get a string which contains either "a" or "b" and I want to get the appropriate member's value. 我得到一个包含“ a”或“ b”的字符串,并且我想获取适当的成员值。

I'd be happy to get some help on that. 我很乐意得到一些帮助。

themesData["a"].key themesData.a.key您的需求,并且与themesData.a.key等效,但“数组索引样式”表示法仍允许您动态生成索引名称。

You can do it in this way: 您可以通过以下方式进行操作:

var member="a"; //or B
var rightMember=themesData[member].key;

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

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