-2

How could I get a variable/property from a string in JavaScript?

The string obj.val returns the value of obj.val. Is adding \" to the beginning and end of the string enough? Thanks! :)

Unlike the post some of you have suggested, the values I want don't have string keys.

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123

1 Answers1

0

you can get value of variable with string name with this[yourString] or with scope[yourString].

var someVar = "Hello world!";
this["someVar"] //this print value of variable name "someVar"

var obj = {}; obj.var = "some object!";
this["obj"]["var"] //this will get value of object property with "var"
Yogen Darji
  • 3,230
  • 16
  • 31