www.webdeveloper.com
Results 1 to 4 of 4

Thread: How do I count 'rows' in a two dimensional array?

  1. #1
    Join Date
    Feb 2012
    Location
    London
    Posts
    8

    How do I count 'rows' in a two dimensional array?

    Hi folks,
    My first post on here so please go easy on me :-)

    I'm declaring a two-dimensional array like this:

    Code:
    var air = new Array();
    air[0] = ['dragonfly', 'crane fly'];
    air[1] = ['eagle', 'falcon'];
    air[2] = ['jumbo jet','harrier'];
    air[3] = [mosquito, wasp];
    etc etc...

    My question is, is there any way to return the number of 'rows' in this array (in this case 4). Have played around with array.length and various 'for' loops but I don't seem to be getting anywhere.

    I'm sure there's an simple answer (as in mysql_num_rows in PHP) but I'm a bit new to javascript and can't see, to find the equivalent.

    Any ideas?

    Thanks in advance,
    Stef

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    778
    If each member of air is considered a row then using length on the air array will give you the number you're looking for.
    You could also define your array like so:

    Code:
    var air = [
    	['dragonfly', 'crane fly'],
    	['eagle', 'falcon'],
    	['jumbo jet','harrier'],
    	['mosquito', 'wasp']
    ];
    
    alert(air.length); //4
    If I'm misunderstanding the question then let me know.
    JavaScript: Learn | Validate | Compact

  3. #3
    Join Date
    Feb 2012
    Location
    London
    Posts
    8
    Hi,
    ...and thanks for getting back to me. No, you've got the question spot on and thanks for the answer. I originally thought I had to stick to the format I gave (the original is churned out of a database by php and comes in that format). Having checked it out though I think I might be able to make what you've shown me work with what I have already so, many thanks.

    Have a good one
    Stef

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    778
    You're welcome.
    JavaScript: Learn | Validate | Compact

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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