Results 1 to 2 of 2

Thread: Objects - How to assign a property name from a string?

  1. #1
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default Objects - How to assign a property name from a string?

    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.
    Last edited by jlizarraga; 11-26-2008 at 08:09 PM. Reason: resolved

  2. #2
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    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.

    Answer: Instead of:

    oObject.oName = oValue;

    It needs to be:

    oObject[oName] = oValue;

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •