SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Set property name of object with var

    This is probably really easy but I cannot figure out how to do it:

    Code JavaScript:
    var newObject = {
        'hello': 'boo' // this is what I want
    };
     
    // The word, "hello" will not always be the same, so I'll set it in a variable:
     
    var x = 'hello2';
     
    // New object with "x" -
     
    var newObject2 = {
        x: 'boo' 
    };
     
    // ^ the above (newObject2) sets property x as "boo" (i.e. newObject.x==='boo')
    // I want there to be a "hello2" property (i.e. newObject2.hello2)
    // How can I make this happen??

    Any help appreciated...
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind, I was being stupid...

    Code JavaScript:
    //Easy:
    var newObject = {};
    newObject[x] = 'boo';
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)


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
  •