Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
-1
rated 0 times [  5] [ 6]  / answers: 1 / hits: 56667  / 14 Years ago, sun, january 30, 2011, 12:00:00

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.


More From » javascript

 Answers
16

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/


[#93991] Thursday, January 27, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
joep

Total Points: 32
Total Questions: 97
Total Answers: 104

Location: Wales
Member since Thu, Jul 1, 2021
3 Years ago
;