8

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

5 Answers 5

9

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?

Sign up to request clarification or add additional context in comments.

5 Comments

Nice. Of course, you'd need an implementation of Array.prototype.reduce for IE < 9
@TimDown Compensated for that (and some more.)
But Uncle Zirak, I still have to work with old IE. Does that mean I'm Amish?
@TimDown Just because all apples are fruit, doesn't make all that's a fruit an apple.
Correct, some fruits can be lemons too!
2

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

Comments

1

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

Comments

0

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;
}

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.