tags:

views:

54

answers:

2

Hi There,

I need some help with Javascript objects. This is what I've got:

_genre_id = {politics:'1',sport:'2',celeb:'3',gossip:'4',busi:'5'};
var genre = 'politics';

What I want to accomplish would be to simply get the value stored for politics, in this case 1 by doing something like this:

var genreID = _genre_id.genre;

But this of course doesn't work because the genre property doesn't exist. I want it to relate to _genre_id.politics.

Any ideas would be welcome.

+13  A: 

How about _genre_id[genre]?

EFraim
+7  A: 
var _genre_id = {politics:'1',sport:'2',celeb:'3',gossip:'4',busi:'5'};
var genre = 'politics';
var genreID = _genre_id[genre];

Note that I added var before setting _genre_id, so that it gets declared in the current scope, without declaring it, you end up setting window._genre_id instead of a local variable.

svinto
Thanks Guys that did the trick!
Conrad