SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to add to this array?

    How do I add a value to this array. I am looping over xml and need to create the following array but dont know how to work with it.

    var projects = [
    {id: '123',label: 'Burger King',desc: 'Burger King Dallas, TX 75233'},
    {id: '234',label: 'Chic Fila',desc: 'Chic Fila Chicago, IL 12255'},
    {id: '345',label: 'Chipotle',desc: 'Chipotle Terrell TX 85211'}
    ];

    Would apprecaite any help.

  2. #2
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I understand,
    As far as I know,
    I wrote this code, İt worked in Firefox.
    Code JavaScript:
    <script type="text/javascript">
     
     
    var projects = [
    {id: "t123",label: "Burger King",desc: "Burger King Dallas, TX 75233"},
    {id: "t234",label: "Chic Fila",desc: "Chic Fila Chicago, IL 12255"},
    {id: "t345",label: "Chipotle",desc: "Chipotle Terrell TX 85211"}
    ];
     
     
    alert(projects[0].id);  //  t123
     
    alert(projects.toSource());
     
    projects[3] = { id: "t103", label:"The Time Through Ages", desc:"In the Name of Allah, Most Gracious, Most Merciful. 1. By the Time, 2. Verily Man is in loss, 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy."};
     
    alert(projects[3].id);      // t103
    alert(projects[3].label);   //  The Time Through Ages
    alert(projects[3].desc);    //  In the Name of Allah, Most Gracious, Most Merciful. 1. By the Time, 2. Verily Man is in loss, 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.
    alert(projects.toSource());
     
    </script>

    http://www.w3.org/TR/html4/types.html
    ID must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To add a value:

    Code JavaScript:
    var projects = [
    {id: '123',label: 'Burger King',desc: 'Burger King Dallas, TX 75233'},
    {id: '234',label: 'Chic Fila',desc: 'Chic Fila Chicago, IL 12255'},
    {id: '345',label: 'Chipotle',desc: 'Chipotle Terrell TX 85211'}
    ];
     
    projects[0].another = 'Another Value';

    That will append "another" with the value of "Another Value"
    Follow me @YaderHernandez


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
  •