How to convert string as object's field name in javascript

javascript

Solution

You can access the properties of javascript object using the index i.e.

var obj = {
  name: 'js',
  age: 20
};

var isSame = (obj["name"] == obj.name)
alert(isSame);

var nameIndex = "name"; // Now you can use nameIndex as an indexor of obj to get the value of property name.
isSame = (obj[nameIndex] == obj.name)

Check example@ : http://www.jsfiddle.net/W8EAr/

Problem

I have a js object like: ``` obj = { name: 'js', age: 20 }; ``` now i want to access name field of obj, but i can only get string 'name', so how to convert 'name' to obj's field name, then to get result like obj.name. Thank you in advance.

Original source