www.webdeveloper.com
Results 1 to 5 of 5

Thread: Multi-Dimensional Array

  1. #1
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    78

    Multi-Dimensional Array

    I have an array, called, "myArray". Inside this myArray are 4 more arrays, "m0, m1, m2, m3".
    How do I access all the elements in all the array?

    Here's my code, :
    Code:
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    
    <body>
    <script>
    var m0 = [0, 1, 2, 3, 4];
    var m1 = [5, 6, 7, 8, 9];
    var m2 = [10, 11, 12, 13, 14];
    var m3 = [15, 16, 17, 18, 19];
    
    var myArray = [m0, m1, m2, m3];
    
    
    for (var i = 0; i < myArray.length; i++) {
        for (var j = 0; j < ("m"+i).length; j++) {
            document.writeln(myArray[i][j]);
        }
    }
    </script>
    </body>
    </html>

    This code only give me the first 2 elements in each array.
    But, I would like to get all the elements in all the arrays.

    tks

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Users do not appreciate simultaneous cross-posting of Mickey Mouse questions like this into every forum you can find. Had you put the search engine to proper use, you could have found plenty of answers without asking.

    https://developer.mozilla.org/en-US/...nsional_Arrays
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648

    Thumbs up

    Just don't say "menu"

    ...and email/phone/whatever else validation ))
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  4. #4
    Join Date
    Dec 2012
    Location
    Prague, Czech Republic
    Posts
    7
    I did this ....

    Code:
    var m0 = [0, 1, 2, 3, 4];
    var m1 = [5, 6, 7, 8, 9];
    var m2 = [10, 11, 12, 13, 14];
    var m3 = [15, 16, 17, 18, 19];
    
    var myArray = [m0, m1, m2, m3];
    var i = 0;
    for (i; i < myArray.length; i++) {
      var result = myArray;
      $('div').html(result);
    }
    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    </head>
    <body>
      <div></div>
    </body>
    </html>
    Here: http://jsbin.com/uyefuf/1/edit/

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Question

    Quote Originally Posted by shinamee View Post
    I did this ....

    Code:
    var m0 = [0, 1, 2, 3, 4];
    var m1 = [5, 6, 7, 8, 9];
    var m2 = [10, 11, 12, 13, 14];
    var m3 = [15, 16, 17, 18, 19];
    
    var myArray = [m0, m1, m2, m3];
    var i = 0;
    for (i; i < myArray.length; i++) {
      var result = myArray;
      $('div').html(result);
    }
    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    </head>
    <body>
      <div></div>
    </body>
    </html>
    Here: http://jsbin.com/uyefuf/1/edit/

    Why bother with the entire 'jquery' library for this: $('div').html(result);

    when all you needed to change was this
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>JS Bin</title>
    </head>
    
    <body>
     <div id="divBox"></div>
    
     <script type="text/javascript">
      var m0 = [0, 1, 2, 3, 4];
      var m1 = [5, 6, 7, 8, 9];
      var m2 = [10, 11, 12, 13, 14];
      var m3 = [15, 16, 17, 18, 19];
    
      var myArray = m0.concat(m1,m2,m3);
      document.getElementById('divBox').innerHTML = myArray;
     </script>
    
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles