Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!