0

Is it possible to access the property of a javascript object using a string?

example: I have a javascript object "obj" that contains a property 'index'.

obj.index = 4;

Now let's say I have a string whose value is the same as a property name of my object:

var str = "index";

Can I use the value of the str variable to access the 'index' property of 'obj'?

user1070447
  • 147
  • 10

2 Answers2

1

Just use the following code:

obj[str]
P. Jairaj
  • 1,033
  • 1
  • 6
  • 8
0

You can use array Notation (squire bracket notation )

obj[str]
Oxi
  • 2,918
  • 17
  • 28