Access JavaScript Object Variable Properties [duplicate]

Possible Duplicate:
How to create object property from variable value in javascript?

How to access property name stored in variable like this?

var obj = {};

obj.foo = 'bar';
var propName = 'foo';

//I want something like this:
console.debug(obj.{propName});

Is there any possibility to do it without using eval()?

Answers:

Answer

You can access the objects properties like:

myObject['property_name']

Try:

var obj = {};
obj.foo = 'bar';
var propName = 'foo';
console.log(obj[propName]);
Answer

Of cource you can:

?(function(){
    var obj = {};
    obj.foo = 'bar';

    var key = 'foo';

    document.write(obj[key]);
})()?

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.