0

I want to pass a variable into a variable

This is what I want

uk.objects.ARG

I have

 var d= ARG

how can I get uk.objects.ARG?

I tried

uk.objects.d

, javascript stores it as the string, not a variable

What should I do in order to make it as a variable ?

user2864740
  • 60,010
  • 15
  • 145
  • 220
user2612912
  • 63
  • 1
  • 6

2 Answers2

4

You can access JavaScript objects' properties also with the bracket notation:

object[property] = value;

The same works for reading. So in your case you could do the following:

var value = uk.objects[d]
tomka
  • 1,275
  • 1
  • 11
  • 17
  • (sidenote) `field` AKA `key` (or *property*) Usually we talk about key-value pairs, not *fields*. – Roko C. Buljan Apr 04 '14 at 01:18
  • (sidenote) *array index notation* is misleading, here we're talking about *properties*, not indexes and specially not *index notation*. Perhaps what you meant to say is `bracket notation`. – Roko C. Buljan Apr 04 '14 at 01:22
  • @RokoC.Buljan: Thanks, your are right. I edited my answer to be more clear. – tomka Apr 04 '14 at 01:29
4

I think what you are looking for is perhaps:

var d = "ARG";

uk.objects[d];
Josh
  • 44,706
  • 7
  • 102
  • 124