-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
  • 507
  • 2
  • 7
  • 19

2 Answers2

1

Use the array access notation.

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

var b = 'BW';
a[b] === 2
J. K.
  • 8,152
  • 1
  • 33
  • 34
-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
  • 356
  • 3
  • 8