0

I have a javascript object (named resource) used for translating my texts.

resource
    .fileNotFound >> "File not found"
    .advSearch >> "Advanced search"

Usually I use this like below:

alert (resource.advSearch);

Now I need to access one of the member of this resource object through a variable.

Example:

var trans = "advSearch";

My question: how can I get the translation in my resource object for 'advSearch' contained in the trans variable?

Thanks.

Bronzato
  • 9,438
  • 29
  • 120
  • 212

2 Answers2

1

You need to use the bracket notation instead of dot notation as the member operator

resource[trans]
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

You can use the [] notation to access properties as well.

resource[trans];
mohkhan
  • 11,925
  • 2
  • 24
  • 27