Is it possible to get length of an array without iterating complete array having non numeric indexes?
Length property is always 0 for non numeric indexed array.
Can we get number of elements using another property of array or it’s prototype?
var arr = [];
arr['first'] = 1;
arr['second'] = 2;
console.log(arr.length); //0
Arrays are meant for numeric indices.
You can add other properties, but they have no impact on the length, and the methods of
Array.prototypewill ignore them.If you’re using non-numeric properties, you should be using
{}(an Object), and you’ll need to maintain your own.length.Alternately, you could use the
Object.keysmethod to find out how many properties there are…For browsers that don’t support ECMAScript 5, you’ll need a compatibility patch…