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

308
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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