-1

Possible Duplicate:
Javascript - access object member when identifier string is stored in a var
javascript object, access variable property name?

I define an object:

var TitlePrice = new Object();
TitlePrice.noEdit   = 1;
TitlePrice.BW       = 2;
TitlePrice.PicPaint = 3;

Now I define a variable, e.g.:

var curren="BW";

How can I access TitlePrice.BW using current variable?, e.g.:

TitlePrice.$current
Community
  • 1
  • 1
dandan
  • 509
  • 3
  • 8
  • 21

2 Answers2

1

Use the array access notation.

var a = {};
a.BW = 2;

var b = 'BW';
a[b] === 2
J. K.
  • 8,268
  • 1
  • 36
  • 35
-1

I'm not sure what you are trying to do. But,

var curren="BW";

is creating a variable and assigning the string "BW" to it.

Also,

TitlePrice.$current

will return undefined because you are not defining a property on your object named $current. If you want the value of TitlePrice.BW to be assigned to the variable curren, you will need to do something like this:

var curren = TitlePrice.BW;
MKS
  • 352
  • 3
  • 8