admin管理员组

文章数量:1322015

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

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());
Share Improve this question edited Sep 27, 2012 at 15:48 Bill the Lizard 406k212 gold badges573 silver badges891 bronze badges asked Sep 27, 2012 at 10:43 theintersecttheintersect 7582 gold badges7 silver badges19 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 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?

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

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

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

本文标签: What is the best way to count absolute array length in JavaScriptStack Overflow