Nir
Nir

Reputation: 4013

Javascript: access to an object's member by name

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.

Upvotes: 8

Views: 6896

Answers (2)

mck89
mck89

Reputation: 19251

You can do it in this way:

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

Upvotes: 5

naivists
naivists

Reputation: 33531

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.

Upvotes: 10

Related Questions