0

I have a couple of check boxes that I'd like to use their "name" attribute as the value I reference within my array.

Let's say I have an array like so:

var test = {
    "one" : 1,
    "two" : 2,
}

var someBtn = "one";

How can I say the following?

test.someBtn; // Which would translate to test.one

Obviously, test.someBtn does not work. Is this even possible?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
txcotrader
  • 585
  • 1
  • 6
  • 21
  • I'm confused. The `test` in your question is an object, not an array. – Zhihao May 14 '13 at 04:11
  • 1
    A better title would be "Access a JavaScript property by a variable" - search for this and there will be *many* duplicates. – user2246674 May 14 '13 at 04:13
  • e.g. http://stackoverflow.com/questions/2241875/how-to-create-object-property-from-variable-value-in-javascript , http://stackoverflow.com/questions/9424885/accessing-object-and-property-via-variable , http://stackoverflow.com/questions/4244896/dynamic-object-property-name – user2246674 May 14 '13 at 04:14

1 Answers1

0

Just refer to it as if it were an array:

test[someBtn]
Coin_op
  • 10,568
  • 4
  • 35
  • 46