SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member DelvarWorld's Avatar
    Join Date
    Jul 2004
    Location
    Baloney
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    push() with a 2d array?

    I have a 2d array that I created like such:
    Code JavaScript:
    var images = new Array(50);
    for(var i=0;i<=51;i++)
    {
    	images[i]=new Array(2);
    }
    Now I would like to push values into this array, but I can't get the proper syntax to work. How would I for example convert these statements:
    Code JavaScript:
    images[1][0]='a';
    images[1][1]='b';
    images[1][2]='c';
    into a push statement (regardless of position pushed into array)?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,803
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    n = images.length;
    images.push(new Array());
    images[n].push='a';
    images[n].push='b';
    images[n].push='c';
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Non-Member DelvarWorld's Avatar
    Join Date
    Jul 2004
    Location
    Baloney
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was kind of hoping the result wouldn't expand my code...
    So it can't be done in one line? You can't push an array into an array?

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    images.push(['a','b','c'])

  5. #5
    Non-Member DelvarWorld's Avatar
    Join Date
    Jul 2004
    Location
    Baloney
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Stereofrog when I executed that code it worked (or at least it didn't throw any errors), but my notation for retrieving elements, images[x][0], images[x][1], images[x][2] doesn't return anything. In fact if I try to alert(images[1][0]) nothing pops up at all.

  6. #6
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var images = [];
    images.push(["a", "b", "c"]);
    images.push(["x", "y", "z"]);
    
    alert(images[0][1]); // b
    alert(images[1][0]); // x
    Hello World

  7. #7
    Non-Member DelvarWorld's Avatar
    Join Date
    Jul 2004
    Location
    Baloney
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, thanks. I guess it was creating a 3d array because I still had the for loop to fill the array with arrays.


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
  •