Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

109
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?

9 months 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);

9 months 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);

9 months 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)

9 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.