theintersect
theintersect

Reputation: 758

What is the best way to count absolute array length in 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());

Upvotes: 8

Views: 295

Answers (5)

Powered
Powered

Reputation: 43

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

return TrueLength;
}

Use this function and get the true array lengths.

Upvotes: 0

Zirak
Zirak

Reputation: 39808

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?

Upvotes: 9

Tim Down
Tim Down

Reputation: 324567

I'd use the in operator:

function getArrayPropertyCount(arr) {
    var count = 0;
    for (var i = 0, len = arr.length; i < len; ++i) {
        if (i in arr) {
            ++count;
        }
    }
    return count;
}

Upvotes: 0

yasaricli
yasaricli

Reputation: 2533

Alternative Prototype method :

Array.prototype.trueLength= function(){
    var list= [], ctr = 0, array = this;

    for(var i in array) (function(arr) {

        if(array.hasOwnProperty(i)){
            list.push(arr);
            ctr++;
        };


    }(array[i]));

    return {length: ctr, "list": list}
}

sample:

var myArray2 =[];
    myArray2[10]='44';
    myArray2[55]='55';

// list not undefined 
myArray2.trueLength().list  // ["44", "55"]

// not undefined length list
myArray2.trueLength().length // 2

Upvotes: 1

Anoop
Anoop

Reputation: 23208

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());

Upvotes: 2

Related Questions