Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to count absolute array length in JavaScript?

Tags:

javascript

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());
like image 673
theintersect Avatar asked Sep 27 '12 10:09

theintersect


People also ask

How do you find the length of an array in JavaScript?

The JavaScript array length property states the number of items in an array. To find the length of an array, reference the object array_name. length. The length property returns an integer.

How do you find the length of an array?

Using sizeof() function to Find Array Length in C++ Hence, if we simply divide the size of the array by the size acquired by each element of the same, we can get the total number of elements present in the array.

What is array length 1 in JavaScript?

length -1 means, specifically the -1 part. When using a for loop over an array we have something like this: for (i = 0; i < array.

How do you set the length of an array?

If you want to change the size, you need to create a new array of the desired size, and then copy elements from the old array to the new array, and use the new array. In our example, arr can only hold int values. Arrays can hold primitive values, unlike ArrayList, which can only hold object values.


2 Answers

Array.prototype.reduce only walks through existing indexes, so you can do:

var length = myArray2.reduce(function(sum) {
    return sum+1;
}, 0);

"But Uncle Zirak! reduce killed my parents!" Don't worry, young one, we can use Array.prototype.filter!

var length = myArray2.filter(function(item, idx) {
    return idx in myArray2;
}).length;

"All this array stuff is boring!" Well, whadya think of this!?

Object.keys(myArray2).length;

"But...but...but Zirak!!!! We're Amish, we don't have ECMAScript 5 yet!" Have no fear, Zirak is here!

for (var length = 0, i = 0; i < myArray2.length; i++) {
    if (i in myArray2) {
        length += 1;
    }
}

But at times like this, one has to wonder: Why do all that and defy the purpose of the array, a structured construct, instead of using something else fit for its purpose?

like image 81
Zirak Avatar answered Sep 28 '22 07:09

Zirak


Iterate through array using for in. jsfiddle

Array.prototype.trueLength= function(){
  var ctr = 0;  
  for(var i in this){
        if(this.hasOwnProperty(i)){
            ctr++;
        }
    }
    return ctr;        
}
console.log(myArray2.trueLength());
like image 21
Anoop Avatar answered Sep 28 '22 08:09

Anoop