Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
How to get the length of a collection?

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

Is there any better way than what I have above?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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
about 4 years ago · Juan Pablo Isaza Relatório

0

When you check the Discord.js docs, in the sidebar under the "Docs Settings" you can select what library's documentation you want to read. By default, it's the main library but you can select Commando, Voice, or Collection too.

By reading the docs, you can see that Collection is just a Map with additional utility methods, and by scrolling down you can see its properties and methods. And there is the .size property you're looking for that returns the number of elements in a Collection.

about 4 years ago · Juan Pablo Isaza Relatório

0

you can use spread operator [...array]

[...collection].length
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda