...

View Full Version : how do you get the amount of non null variables an array



kwhubby
11-08-2002, 12:43 AM
how do you get the amount of non null variables an array.
In my example, I use x.length , but that gives the length of the entire array, how do I get it to tell me how many have been filled

var x = new Array(10)
x[0] = "blah blah blah''
x[1] = "ha ha ha"
x[2] = "I dont care"
alert(x.length) although this gives me what I dont want which is 10
I want it to tell me 3
:confused:

chrismiceli
11-08-2002, 12:47 AM
you could make a loop like this
x = 0
for (i=0, i<a.length, ++i) {
if (a[i] != "") ++x
}
that should give the number of values of that are not null or undefined
<edit> you might have to change "" to "undefined" the objects aren't null because you made them, just not defined yet.</edit>

kwhubby
11-08-2002, 01:08 AM
yah ok thanks, I was hoping there was a really simple way such as x.notnulllength or something , but oh well Ill just use a for statement. Thanks, alot



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum