Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
Listing an array in a message

I want to list an array in a discord message. I've got the following code so far:

message.channel.send("Game is starting with " + playerNumber + " Players! \n"
    + memberIDs.forEach(element => ("<@" + element + "> \n")));

The memberIDs array contains all IDs from member in a specific channel, now I want to put all member who are in the channel into a message. I've tried it with the code above but it only gives me the message:

Game is starting with *playerNumber* Players!
undefined

I don't really understand why it is undefined because it also would give me the memberIDs in a console log or when I make a forEach loop with messages that get created for every memberID.

The Array in the console.log locks like the following:

[ '392776445375545355', '849388169614196737' ]

I appreciate any help.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

short answer
forEach() is not your friend; you need to map() and join()

long answer
the undefined is the result of the forEach() invocation which does not return anything.

so you need to get an string instead.

to do so, you may use .map() which returns an array; and .join() that joins an array of strings into an string.

something like this:

memberIDs.map(element => "<@" + element + ">" ).join('\n');
about 4 years ago · Juan Pablo Isaza Denunciar

0

forEach iterates over an array and doesn't return anything. Use map instead with join:

message.channel.send("Game is starting with " + playerNumber + " Players! \n"
    + memberIDs.map(element => `<@${element}>`).join('\n')
);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Array#map() returns an array of return values from the callback, while Array#forEach() returns undefined. Use .map()

memberIDs.map(m => `<@${m}>`)
.join("\n") // join each element with a newline
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda