最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: access to an object's member by name - Stack Overflow

programmeradmin5浏览0评论

I have an object called 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.

I'd be happy to get some help on that.

I have an object called 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.

I'd be happy to get some help on that.

Share Improve this question edited May 15, 2014 at 13:18 Germstorm 9,84914 gold badges69 silver badges83 bronze badges asked Jan 25, 2010 at 14:09 NirNir 4,0138 gold badges39 silver badges53 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

themesData["a"].key does what you need and is equivalent to themesData.a.key, still the "array index style" notation allows you to dynamically generate index names.

You can do it in this way:

var member="a"; //or B
var rightMember=themesData[member].key;
发布评论

评论列表(0)

  1. 暂无评论