Home > database >  How to get the length of a collection?
How to get the length of a collection?

Time:11-02

I'm trying to find the most optimal way to get the length of a collection. Currently I'm turning the collection into an array, and then getting the length of that. But the Discord.js docs specifically state not to use this method unless there is absolutely no way to do this using the collection. So far, I've found no other way to get the length. Here is my code to find the length:

Array.from(collection.values()).length

So is there any better way than what I have above?

CodePudding user response:

I am not 100% sure what is the 'collection' type, but I assume that it is either Set or Map.

In that case both have property size which returns current collection size:

collection.size

CodePudding user response:

you can use spread operator [...array]

[...collection].length
  • Related