Javascript:按名称访问对象的成员 [英] Javascript: access to an object's member by name

查看:137
本文介绍了Javascript:按名称访问对象的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为的对象themesData

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

...我希望通过其名称访问其中一个成员。我得到一个包含a或b的字符串,我希望获得相应成员的价值。

...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.

推荐答案

themesData [a] .key 做你做的需要并且相当于 themesData.a.key ,仍然数组索引样式表示法允许您动态生成索引名称。

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.

这篇关于Javascript:按名称访问对象的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆