SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2005
    Posts
    456
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    arrays in arrays

    hi all,
    im trying to do an array within another array (it needs to be done this way and not with a multidimensional array). ive got this code that works:
    var timecard = new Array();
    timecard = [ ["Monday", 8],
    ["Tuesday", 8],
    ["Wednesday", 7],
    ["Thursday", 9],
    ["Friday", 8] ];

    // Display the contents of timecard.
    for(var i=0; i<timecard.length; i++){
    document.write("Worked " + timecard[i][1] + " hours on " + timecard[i][0] + "." + "<br />");
    }

    but how can i add to this array dynamically? for example somthing like this:
    for(var i=0; i<5; i++){
    timecard += ["Monday", 8]
    // the above line is just adding 5 "monday" elements to the array for now.
    }

    do i need to add the comma between the newly added array elements? heres the link i got the above code from:
    http://msdn2.microsoft.com/en-us/lib...sc(VS.80).aspx

    many thanks.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var newThing = ["Monday",8];
    timecard.push(newThing);

  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2005
    Posts
    456
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks jim,
    very helpful
    - D


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
  •