faqts : Computers : Programming : Languages : JavaScript : Language Core : Arrays

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

13 of 30 people (43%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How to find the array lenght when the elements are inserted with keys(ex: cars["fast"]="Mustang")?

Jun 12th, 2002 14:59
David Blackledge, Long Shot,


For an explanation of what's going on, see this answer:
http://www.faqts.com/knowledge_base/view.phtml/aid/13847/fid/144
To figure out the length, you'll have to count the properties by hand.  
Luckily, the for..in statement is available to help you with this.  
Since you're using an Array object, you have some idea of what 
properties the object already has and can be sure to ignore them.
var keylength = 0;
for(var x in myArray) ++keylength; // just count them.
// now, subtract all of the integer indexed items
// which are counted by the real array length
keylength -= myArray.length;
// now, subtract one for the "length" property
// which isn't one of your real keys
keylength -= 1;
If you want a length that includes the integer index items as well, then 
don't subtract the myArray.length.
David.
http://David.Blackledge.com