# Add a way of checking the actual number of items in an array

**URL:** <https://es.discourse.group/t/add-a-way-of-checking-the-actual-number-of-items-in-an-array/1080>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [November 14, 2021, 3:26am UTC](https://es.discourse.group/t/add-a-way-of-checking-the-actual-number-of-items-in-an-array/1080 "2021-11-14T03:26:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zade](https://avatars.discourse-cdn.com/v4/letter/z/d6d6ee/32.png) [@zade](https://es.discourse.group/u/zade)\
**Post date:** [November 14, 2021, 3:26am UTC](https://es.discourse.group/t/add-a-way-of-checking-the-actual-number-of-items-in-an-array/1080/1 "2021-11-14T03:26:18Z")

</div>

I was recently working on a project when I noticed that `Array.prototype.length` doesn't actually seem to be the length of the array. Rather it's the highest index in the array plus one. Looking into this some more, it seems like this is the intended behaviour, which strikes me as bizarre.

I suggest adding an new property to arrays: `Array.prototype.size`. `Array.prototype.size` would represent the actual number of items in the array, excluding empty spaces. The reason that i'm suggesting that this property be called `size` is because that is what's used in other constructs like `Map`s.  
This may have already been suggested, and if so, I apologise.

Here's an example implementation:

```javascript
Object.defineProperty(Array.prototype, "size", {
    get: function() {
        let counter = 0;
        this.forEach(() => counter++)
        return counter;
    }
})

```

And here are some examples of using it, run in Edge 98:

```javascript
[].size // -> 0
[].length // -> 0

[1,2].size // -> 2
[1,2].length // -> 2

[1,,2].size // -> 2
[1,,2].length // -> 3

```

What are your thoughts on this?

---

<div class="post-metadata">

**Author:** ![simonkcleung](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/simonkcleung/32/785_2.png) [@simonkcleung](https://es.discourse.group/u/simonkcleung)\
**Post date:** [November 14, 2021, 9:56am UTC](https://es.discourse.group/t/add-a-way-of-checking-the-actual-number-of-items-in-an-array/1080/2 "2021-11-14T09:56:50Z")

</div>

```javascript
Object.defineProperty(Array.prototype, "size", {
    get: function() {
        return Object.keys(this).length;
    }
})

```

do the same thing?

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [November 14, 2021, 11:40am UTC](https://es.discourse.group/t/add-a-way-of-checking-the-actual-number-of-items-in-an-array/1080/3 "2021-11-14T11:40:03Z")

</div>

From [Array.from and sparse arrays](https://esdiscuss.org/topic/array-from-and-sparse-arrays#content-2:)

> At the last TC39 meeting, we agreed holes are freakish and should be discounted in designing new APIs like Array.from (I think; I may be overstating slightly, but that's the effect).

I don't think the general sentiment has changed since then. Just don't use sparse arrays, you won't need `.size` then.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [November 14, 2021, 11:43am UTC](https://es.discourse.group/t/add-a-way-of-checking-the-actual-number-of-items-in-an-array/1080/4 "2021-11-14T11:43:11Z")

</div>

No, it won't do the same thing. That would also count non-index properties, and e.g. give the wrong size for `Object.assign([], {example: "boom!"}).size`.

A similarly simple implementation would be

```
 return this.filter(() => true).length;

```

but `forEach` is probably more efficient.

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [November 17, 2021, 9:14am UTC](https://es.discourse.group/t/add-a-way-of-checking-the-actual-number-of-items-in-an-array/1080/5 "2021-11-17T09:14:27Z")

</div>

More like `Object.keys(this).filter(k => /^\d+$/.test(k)).length`, but yes.

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [November 17, 2021, 10:24am UTC](https://es.discourse.group/t/add-a-way-of-checking-the-actual-number-of-items-in-an-array/1080/6 "2021-11-17T10:24:12Z")

</div>

There's no easy way to count array elements from `Object.keys`. `"007"` and `"123456789012"` are both valid object keys that can be set on the array, but are not elements.

A bit surprising nobody said `arr.reduce(n=>n+1, 0)` ;)

---

<div class="post-metadata">

**Author:** ![zade](https://avatars.discourse-cdn.com/v4/letter/z/d6d6ee/32.png) [@zade](https://es.discourse.group/u/zade)\
**Post date:** [November 18, 2021, 12:36am UTC](https://es.discourse.group/t/add-a-way-of-checking-the-actual-number-of-items-in-an-array/1080/7 "2021-11-18T00:36:25Z")

</div>

I agree that holes in arrays are a terrible thing that should never have existed, but I still think that this is an important feature. I know that I've had several headaches that would have been avoided if there was a simple way to check the actual number of items in an array natively in the language, and I'm sure that lots of other people feel the same.  
At the moment I have to import a helper function every time I want to count the number of items in an array (so that I don't need to modify the prototype), which seems like something that just shouldn't need to be done.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [November 18, 2021, 2:01am UTC](https://es.discourse.group/t/add-a-way-of-checking-the-actual-number-of-items-in-an-array/1080/8 "2021-11-18T02:01:13Z")

</div>

Newer EcmaScript APIs Often treat arrays with holes, and arrays filled with undefined as the same thing. For example:

```javascript
> [...[2,,3].values()] // An array with a whole
[2, undefined, 3]
> [...[2,undefined,3].values()] // An array with undefined
[2, undefined, 3]

```

If others follow suit and also design their APIs to treat holes like undefined, then I don't think there's a reason to know the number of non-hole elements in an array, as a "hole" should ideally be indistinguishable from an undefined entry from the API's perspective.

But, perhaps, would you be able to elaborate on one of your use cases? So we can have a better idea of why this would be valuable?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [November 18, 2021, 2:37am UTC](https://es.discourse.group/t/add-a-way-of-checking-the-actual-number-of-items-in-an-array/1080/9 "2021-11-18T02:37:56Z")

</div>

Exactly this. The proper number of items in an array to think about, sparse or otherwise, is the length. Just pretend holes don’t exist.
