/    Sign up×
Community /Pin to ProfileBookmark

Array length discrepancy

[CODE]
var test = [

[],
[],
[],

];

alert(test.length);
[/CODE]

The above code simply shows me the length of the array test.

When I run this in IE, I get 4.
When I run this in Firefox, I get 3.

WHY!!!

I admit the code seems a little unusual and arbitrary, but its the setup of a much more complicated multi-dimensional array for a complex menu code which I did not write and thus I have little choice of the input for the menu. Again for this more complicated array the two browsers gives me different lengths.

I have seen many browser differences before, some seemingly absurd – but this takes the biscuit! How can an array be both 3 and 4 in length!

Any help would be much appreciated, so I can finish my work to make my site fully cross-browser compliant.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@FangNov 02.2004 — It should give an error as the last element should not have a comma
Copy linkTweet thisAlerts:
@senshiNov 02.2004 — I would imagine that its seeint it as an array containing an array of elements as you hev basically said the same as

var thisArray=[[],[],[]];

which depending on how the browser sees it could be [],[],[],[] or [][],[][],[][] 3 multidimentional arrays.
Copy linkTweet thisAlerts:
@CharlesNov 02.2004 — It's the comma.
Copy linkTweet thisAlerts:
@andyfrithauthorNov 02.2004 — Thanks for the help - the comma is indeed the problem.

What is the best way to remove it dynamically? When I used the slice method of the Array class to do it:

it removed a "real" element, not one created by extra commas. (As I said, the real array is a complicated beast, much worse than my example)

Should I convert it to a string and remove the comma(s) at the end using string methods?
Copy linkTweet thisAlerts:
@7studNov 02.2004 — but its the setup of a much more complicated multi-dimensional array for a complex menu code which I did not write and thus I have little choice of the input for the menu.[/quote]

Why can't you do this?

var test = [ [null], [null], [null] ];

nevermind..
Copy linkTweet thisAlerts:
@andyfrithauthorNov 02.2004 — I could send you the full menu input array - but I doubt you'd be happy to go through its structure... plus I'm currently dealing with a subset of that array, which is not uniform in its multi-dimensionality!!

IE its not a matrix type thing, but each array within the array has different lengths
×

Success!

Help @andyfrith 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.20,
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,
)...