Just wondering, when you use array.length it gets the last index value and adds one. What if you have an array, that is defined this way for some reason:
var myArray2 =[];
myArray2[10]='x';
myArray2[55]='x';
What is the absolute best way to get the true length of this Array? Something that would return 2 as the value.
I was thinking something like this, but not sure if there was already a method for this, or if there is a faster implementation.
Array.prototype.trueLength= function(){
for(var i = 0,ctr=0,len=myArray2.length;i<len;i++){
if(myArray2[i]!=undefined){
ctr++;
}
}
return ctr;
}
console.log(myArray2.trueLength());