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

341
Vistas
JavaScript - Loop Through Array Directly In Multi Line String and Include Values

Let's say I have an array called users.

const users = ["userOne", "userTwo"];

Within an ES6 multi-line string, I would like to loop through the array and include the values of the users array within the multi-line string.

I have tried giving it a go using the following code snippet:

const data = `

Hello,

The following users exist within the users array:

- ${users.forEach((item) => item)},
`

The expected outcome I would like it something like this:

Hello,

The following users exist within the users array:

- userOne
- userTwo

What would I need to do to loop through the array and include the values within the string?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You could map the items with some formatting.

const users = ["userOne", "userTwo"];
const data = `Hello,

The following user${users.length > 1 ? 's' : ''} exist within the user's array:

${users.map(item => `- ${item}`).join(',\n')}`

console.log(data);

over 4 years ago · Santiago Trujillo Denunciar

0

You could try:

const users = ["userOne", "userTwo"];

const data = `Hello, The following users exist within the users array:
${users.map((user) => {
    return '- ' + user + '\n';
})}`

console.log(data);

over 4 years ago · Santiago Trujillo Denunciar

0

Use map instead of forEach and join with delimitter '- '

const users = ["userOne", "userTwo"];


const data = `

Hello,
The following users exist within the user's array:

- ${users.map(item => item +'\n').join('- ')}`;

console.log(data)

over 4 years ago · Santiago Trujillo 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