Array.size() is not a valid method
Always use the length property
There is a library or script adding the size method to the array prototype since this is not a native array method. This is commonly done to add support for a custom getter. An example of using this would be when you want to get the size in memory of an array (which is the only thing I can think of that would be useful for this name).
Underscore.js unfortunately defines a size method which actually returns the length of an object or array. Since unfortunately the length property of a function is defined as the number of named arguments the function declares they had to use an alternative and size was chosen (count would have been a better choice).
Find Array length in Javascript - Stack Overflow
Why is getting the length of an array O(1) and not O(n)?
Are rhere any simpler ways to measure length of an array in JS?
How JavaScript Array Works Internally?
Videos
Array.size() is not a valid method
Always use the length property
There is a library or script adding the size method to the array prototype since this is not a native array method. This is commonly done to add support for a custom getter. An example of using this would be when you want to get the size in memory of an array (which is the only thing I can think of that would be useful for this name).
Underscore.js unfortunately defines a size method which actually returns the length of an object or array. Since unfortunately the length property of a function is defined as the number of named arguments the function declares they had to use an alternative and size was chosen (count would have been a better choice).
.size() is not a native JS function of Array (at least not in any browser that I know of).
.length should be used.
If
.size() does work on your page, make sure you do not have any extra libraries included like prototype that is mucking with the Array prototype.
or
There might be some plugin on your browser that is mucking with the Array prototype.
What you are looking for is not the length of an array but the number values allocated in that array.
Array.length will NOT give you that result but the total number of values allocated.
A workarround is to count the properties of the object behind the array, with:
Object.keys(a).length
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Relationship_between_length_and_numerical_properties
But with some caveats:
- It will also count literal properties, like a.a_property. I do not think that is what you want. So, you will have to filter that result:
!(+el % 1) which check if el can be considered as numerical property even if it has a type of string.
- you want count only positive integers, so you have to filter them with:
+el>=0
- finally, as array size is limited to 2^32, you will to also filter positive integers greater than that:
+el < Math.pow(2,32)
Functionally, you will have your result with this filter:
Array.realLength= Object.keys(a).filter(function(el){return !(+el % 1) && +el>=0 && +el < Math.pow(2,32) ;}).length
TL;DR The simplest reliable approach that I can think of is the following:
var count = a.filter(function() { return true; }).length;
In modern JavaScript engines, this could be shortened to:
var count = a.filter(() => true).length;
Full answer:
Checking against undefined isn't enough because the array could actually contain undefined values.
Reliable ways to find the number of elements are...
Use the in operator:
var count = 0;
for (var i = 0; i < a.length; i += 1) {
if (i in a) {
count += 1;
}
}
use .forEach() (which basically uses in under the hood):
var a = [1, undefined, null, 7];
a[50] = undefined;
a[90] = 10;
var count = 0;
a.forEach(function () {
count += 1;
});
console.log(count); // 6
or use .filter() with a predicate that is always true:
var a = [1, undefined, null, 7];
a[50] = undefined;
a[90] = 10;
var count = a.filter(function () { return true; }).length;
console.log(count); // 6
I have read that this operation works in constant time. But how is that possible?
Given an array of length n, I would have thought that computing n would require accessing every element. In pseudocode, something like the following:
n = 0
for each item in array:
n = n + 1
return nBut that would clearly be O(n), suggesting that’s not how it actually works.
Can anyone shed light on this?