PDA

View Full Version : Resolved Objects - How to assign a property name from a string?



jlizarraga
11-26-2008, 07:56 PM
Hi all,

Pretty simple question today. I'm trying to create a new property/value pair in an Object from a couple variables (which are strings), but this is the result I'm getting:

var oObject = new Object();
var oName = "someProperty";
var oValue = "someValue";
oObject.oName = oValue;
for (var i in oObject){
alert(i); // Alerts "oName"
alert(oObject[i]); // Alerts "someValue"
}

How can I get the first alert to show "someProperty" instead of "oName"?

Thanks a ton guys.

jlizarraga
11-26-2008, 08:07 PM
Step 1: Have stupid problem.
Step 2: Post it on DD.
Step 3: The answer magically comes to you and you look like a tard. :D

Answer: Instead of:

oObject.oName = oValue;

It needs to be:

oObject[oName] = oValue;