/    Sign up×
Community /Pin to ProfileBookmark

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];[/CODE]

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 [B]array.length[/B] and various ‘for’ loops but I don’t seem to be getting anywhere.

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

Any ideas?

Thanks in advance,
Stef

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@bionoidFeb 16.2012 — If each member of [B]air[/B] is considered a row then using [B]length[/B] on the [B]air[/B] 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); [COLOR="Red"]//4[/COLOR][/CODE]


If I'm misunderstanding the question then let me know.
Copy linkTweet thisAlerts:
@mxbrainwrongauthorFeb 16.2012 — 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
Copy linkTweet thisAlerts:
@bionoidFeb 16.2012 — You're welcome.
×

Success!

Help @mxbrainwrong spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...