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

146
Visualizações
discordjs return total users in forEach?

I did try this code :

message.client.guilds.cache.forEach((guild) => {
     console.log(guild.memberCount)
})

and it returns like this:

300
200
100
etc

How to get a total in one go like them all together so it shows up like 600 instead all seperate. I know there is an other code that shows all the users in the guild, but i just want to know how to do it this way, so i can expand the code and filter them on presence.

i just dont know how to total the value in a forEach or map

regards

//UPDATE I need to get it working like this method, this shows like the above log: 100 200 300

  client.guilds.cache.forEach((guild) => {
     const total = guild.members.cache.filter(member => member.presence?.status == "online").size
     console.log("per 1", total)
    
    
     
     })
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can write a function like:

   function arrSum(arr){
        total = 0;  
        arr.forEach(function(key){
            total = total + key;            
        });
        return total;
    }

and use it like:

arrSum(message.client.guilds.cache);
about 4 years ago · Juan Pablo Isaza Relatório

0

You could use map + reduce chaining...you can retrieve the 'memberCount' with the mapping and summing the actual values with the reduce:

    const guilds = [
    { memberCount: 100 },
    { memberCount: 200 },
    { memberCount: 300 },
    { memberCount: 400 }
];

const totalMembers = guilds
  .map((guild) => guild.memberCount)
  .reduce((prev, curr) => prev + curr, 0);

console.log(totalMembers);
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the Array.reduce() method.

const totalUsers = message.client.guilds.cache.map(el => el.memberCount).reduce((a,b) => a+b)
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