SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2010
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Multidimensional Array Length

    Hi.

    Is there any function or property for finding out the size of the first (or for that matter any) dimension of a multidimensional array?

    EDIT:

    There seems to be no such functionality, I found a solution that does not require it. If anyone is reading this for the same reason; it needs to be scripted.

  2. #2
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I understand

    Code JavaScript:
    <script type="text/javascript">
     
    var A = [
     
    ["The Time Through Ages","In the Name of Allah, Most Gracious, Most Merciful,","1. By the Time,","2. Verily Man is in loss,"," 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy."],
     
    ["The Opening", "1. In the name of Allah, Most Gracious, Most Merciful.", "2. Praise be to Allah, the Cherisher and Sustainer of the worlds;", "3. Most Gracious, Most Merciful;", "4. Master of the Day of Judgment.", "5. Thee do we worship, and Thine aid we seek.", "6. Show us the straight way,", "7. The way of those on whom Thou hast bestowed Thy Grace, those whose (portion) is not wrath, and who go not astray."]
     
    ]
     
    alert(A.length);        // 2 
    alert (A[0].length);    // 5
    alert (A[0][0]);        // The Time Through Ages 
    alert(A[0][0].length);  // 21
    alert (A[1][0]);        // The Opening
    alert(A[1][0].length);  // 11
     
     
    var s = "";
    var t = "<table border=2 ><tbody>";
     
    for(var i=0; i< A.length; i++) {
     
    t +="<tr><td>" + A[i].length + "</td>";
     
    var sum = 0;
     
    for(var k= 0; k < A[i].length; k++){
     
    sum += A[i][k].length;
     
    t += "<td>" + A[i][k] + "-----" + A[i][k].length + "</td>";
     
    }
     
    t += " <td>" +sum+"</td></tr>";
     
    }
     
    t+= "</tbody></table>";
     
     
    document.write(t);
     
    </script>
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Since JavaScript doesn't have multi-dimensional arrays there is no such thing as multi-dimensional array length in JavaScript.

    Each element in a JavaScript array can itself be an array giving the equivalent of a multi-dimensional array but the lengths of the arrays iyou add for the second dimension are not forced to be the same and so while you have one length for the first dimentsion of the array each of those elements has its own separate length for the second dimension and so on for as many more arrays within arrays as you build.

    So if your array is:

    var arry = [1
    ,[1,2,3]
    ,[1,2]
    ,[[1,2,3],[4,5,6]]
    ];

    the length of the first dimension is arry.length which will return 4.

    The length of the second dimension depends on which element of the first array you are referring to. The first element isn't an array and so doesn't have an array length. The second arry[1].length is 3 and the third and fourth arry[2].length and arry[3].length are both 2.

    The last element of the array has a third dimension with both arry[3][0].length and arry[3][1].length being 3.
    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="^$">


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
  •