Results 1 to 3 of 3

Thread: how do i populate this multudimension array?

  1. #1
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default how do i populate this multudimension array?

    hi
    new to multi dimension arrays and not very bright.

    who do i populate a multidimension array from a single string?
    the string is formatted like this:
    Code:
    var resultVar = "id1,photo1,thumb1,description1,id2,photo2,thumb2,description2,id3,photo3,thumb3,description3";
    thanks

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <script type="text/javascript">
    /*<![CDATA[*/
    var resultVar = "id1,photo1,thumb1,description1,id2,photo2,thumb2,description2,id3,photo3,thumb3,description3";
    
    var split=resultVar.split(',');
    var ary=[];
    var length=4;
    
     for (var nu,z0=0;z0<split.length;z0++){
      nu=Math.floor(z0/length);
      if (!ary[nu]){
       ary[nu]=[];
      }
      ary[nu].push(split[z0]);
     }
    
    alert(ary.join('\n'));
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    okay, i can see the alert with the values divided, i assume i will be able to understand how to use your code. thanks.

    but for sake of me learning how to do this in simpler terms which i understand, how would i finish the following code?

    Code:
    <script type="text/javascript">
    
    var resultRaw = "3,1a_2a_3a_4a-1b_2b_3b_4b-1c_2c_3c_4c";
    var charAtPos = resultRaw.indexOf(',');
    var items = resultRaw.substring(0, charAtPos);
    var resultVar = resultRaw.substr(charAtPos + 1);
    
    var simpleArray = resultVar.split("-");
    // simpleArray[0] = 1a_2a_3a_4a;
    // simpleArray[1] = 1b_2b_3b_4b;
    // simpleArray[2] = 1c_2c_3c_4c;
    // simpleArray[3] = 1d_2d_3d_4d;
    
    
    var rowArray = simpleArray[0].split("_");
    // rowArray[0] = 1a;
    // rowArray[1] = 2a;
    // rowArray[2] = 3a;
    // rowArray[3] = 4a;
    
    </script>
    i'd like to end up with something like

    Code:
    dataArray[0][0] = 1a;
    dataArray[0][1] = 2a;
    dataArray[0][2] = 3a;
    dataArray[0][3] = 4a;
    dataArray[1][0] = 1b;
    dataArray[1][1] = 2b;
    //etc..
    i like the fact that the split automatically creates an array, i just havent found out how to implement it yet in a 2d array.
    Code:
    var newArray = somestring.split(",");
    EDIT

    nevermind, figured it out.
    Last edited by ricmetal; 06-19-2011 at 09:10 AM.

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
  •