SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: 2D Array

  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    2D Array

    I want the results of 2 dimension array in below table.
    file_a_0 file_a_1 file_a_2 file_a_3 file_a_4 file_a_5 file_a_6 file_a_7 file_a_8 file_a_9
    file_b_0 file_b_1 file_b_2 file_b_3 file_b_4 file_b_5 file_b_6 file_b_7 file_b_8 file_b_9
    file_c_0 file_c_1 file_c_2 file_c_3 file_c_4 file_c_5 file_c_6 file_c_7 file_c_8 file_c_9
    file_d_0 file_d_1 file_d_2 file_d_3 file_d_4 file_d_5 file_d_6 file_d_7 file_d_8 file_d_9
    file_e_0 file_e_1 file_e_2 file_e_3 file_e_4 file_e_5 file_e_6 file_e_7 file_e_8 file_e_9
    file_f_0 file_f_1 file_f_2 file_f_3 file_f_4 file_f_5 file_f_6 file_f_7 file_f_8 file_f_9

    Below the cord is worked when it is one dimensional array, but it is changed into 2D dimensional array, it doesnít work. I try many times and still canít resolve it.

    var fStep=0
    var fPath
    var Mslide = new Array("file_a_", "file_b_", "file_c_", "file_d_", "file_e_", "file_f_")
    for (fPath=0, fPath<=Mslide.length, fPath++){
    Mslide[fPath] = new Array(10);
    do{
    if (fStep>=0 )
    Mslide[fPath][fStep]=fPath + "00" + fStep + ".jpg"

    return Mslide[fPath][fStep]
    fStep=fStep+1
    }
    while ( fStep<Mslide[fPath].length )
    document.write(Mslide[fPath][fStep])
    document.write("<br>")
    }
    }
    Please help!

  2. #2
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <html>
    <head>
    <title>tablo yaptır</title>
    
    <script type="text/javascript">
    var arr = ["a","b","c","d","e","f"];
    
    document.write('<table border="1" id="tableid" cellpadding="5" bgcolor="#00FF66">')     
    for(i=0;i<6;i++){     
    document.write('<tr>')  
    for(k=0;k<10;k++){       
    document.write('<td>'+arr[i]+k+'</td>')}
    
    document.write('</tr>')}
    document.write('</table>')
    
    </script>
    </head>
    <body bgcolor="white" text="black" link="blue" vlink="purple" alink="red">
    <p></p>
    </body>
    </html>

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Are you trying to put that data into a 2D-array or is it already in such an array and you are trying to display its contents? I can't really tell.

  4. #4
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for you both help.
    I'm sorry, I don't clearly display my questions.
    I have 2D array to display its contents, same as:
    file_a_0
    file_a_1
    file_a_2
    ...
    file_b_0
    file_b_1
    file_b_2
    ...
    file_c_0
    file_c_1
    file_c_2
    ...

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    OK, but does the array already exist? This is how it should be:
    Code:
    var arr = [
      ['file_a_0', 'file_a_1', 'file_a_2', 'file_a_3', 'file_a_4', 'file_a_5', 'file_a_6', 'file_a_7', 'file_a_8', 'file_a_9'],
      ['file_b_0', 'file_b_1', 'file_b_2', 'file_b_3', 'file_b_4', 'file_b_5', 'file_b_6', 'file_b_7', 'file_b_8', 'file_b_9'],
      ['file_c_0', 'file_c_1', 'file_c_2', 'file_c_3', 'file_c_4', 'file_c_5', 'file_c_6', 'file_c_7', 'file_c_8', 'file_c_9'],
      // and so on for the other three
    ];
    Then loop through it like this:
    Code Javascript:
    for (var i = 0; i < arr.length; i++) {
      var arr2 = arr[i];
      for (var j = 0; j < arr2.length; j++) {
                      document.write(arr2[j]);
      }
    }
    However, since the pattern is so repetitive, you can use something like what muazzez posted and just forget about creating the array. But it's difficult to tell what you actually need because you're still being unclear about it.


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
  •