Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.length gives incorrect length

If I have an array having object as values at the indices like:

var a = []; a[21] = {}; a[90] = {}; a[13] = {}; alert(a.length); // outputs 91 

I have found a workaround to get the actual length:

function getLength(arr) {     return Object.keys(arr).length; } var a = []; a[21] = {}; a[90] = {}; a[13] = {}; alert(getLength(a)); 

But, why does JS gives incorrect length when objects are stored at random indices? It just adds 1 to the largest index found on an array. Like in the above example, 90 was the largest index. It just adds 1 and gives 91 as output. Demonstration

like image 284
Muhammad Talha Akbar Avatar asked Jun 26 '15 04:06

Muhammad Talha Akbar


People also ask

What is invalid array length?

The JavaScript exception "Invalid array length" occurs when specifying an array length that is either negative, a floating number or exceeds the maximum supported by the platform (i.e. when creating an Array or ArrayBuffer , or when setting the Array.

What is length in array length?

length is a property of arrays in JavaScript that returns or sets the number of elements in a given array. The length property of an array can be returned like so. The assignment operator, in conjunction with the length property, can be used to set the number of elements in an array like so.

Is array length same as zero length?

Is there any difference between checking an array's length as a truthy value vs checking that it's > 0? Since the value of arr. length can only be 0 or larger and since 0 is the only number that evaluates to false , there is no difference.

How do you declare the length of an array?

The usual way of declaring an array is to simply line up the type name, followed by a variable name, followed by a size in brackets, as in this line of code: int Numbers[10]; This code declares an array of 10 integers.


1 Answers

That's because length gives you the next index available in the array.

DOCS

arrayLength

If the only argument passed to the Array constructor is an integer between 0 and 2^32-1 (inclusive), this returns a new JavaScript array with length set to that number.

ECMA Specifications

Because you don't have inserted any element in the other keys than 21, 90, 13, all the remaining indexes contains undefined. DEMO

To get actual number of elements in the array:

var a = [];  a[21] = {};  a[90] = {};  a[13] = {};    var len = 0;    for (var i = 0; i < a.length; i++) {    if (a[i] !== undefined) {      len++;    }  }  document.write(len);

Shorter version

var a = [];  a[21] = {};  a[90] = {};  a[13] = {};      for (var i = 0, len = 0; i < a.length; i++, a[i] !== undefined && len++);      document.write(len);

DEMO

EDIT

If the array contains large number of elements, looping to get its length is not the best choice.

As you've mentioned in the question, Object.keys(arr).length is the best solution in this case, considering that you don't have any properties added on that array. Otherwise, the length will not be what you might be expecting.(Thanks To @RobG)

like image 171
Tushar Avatar answered Oct 01 '22 04:10

Tushar