Monday 23 April 2012

Javascript array length problem

There is a problem in javascript array.length property.

var myOtherArray = [];
myOtherArray[100] = ‘one’;
and its length property would actually return 101 instead of 1.


Use this function  for correct result ,
function count(array)
{
   var c = 0;
   for(i in array) // in returns key, not object
   if(array[i] != undefined)
   c++;
   return c;
}

No comments:

Post a Comment

Please share your thought about this