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

197
Views
How to generate HTML text with a for loop?

I have a list of expenses, I want to create a html code to iterate over all the expenses and show their name. I am not working with the DOM, I literally want to save the html code in a variable, so I can generate a pdf file with it.

This is what I tried: lets say I have this array

const spents = [{expenseName: "Pizza"},{expenseName: "Home"}]

    const testHtml = () => {
         for(let i of spents) {
            const title = `<h1>${i.expenseName}</h1>`
          }
        }
        testHtml()

This is the result I want, something like:

htmlResult = "<h1>${i.expenseName}</h1> <h1>${i.expenseName}</h1>"

by the way, This is for a react native app.

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

I think this will work for you.

const spents = [{expenseName: "Pizza"},{expenseName: "Home"}]

const testHtml = () => {
   let title = '';
   for(let i of spents) {
       title += `<h1>${i.expenseName}</h1>`
   }
   return title;
}
testHtml()
about 4 years ago · Santiago Gelvez Report

0

You could use Array.prototype.reduce().

const spents = [{
  expenseName: "Pizza"
}, {
  expenseName: "Home"
}];

const result = spents.reduce((prev, curr, index) => index === 0 ? curr.expenseName : `<h1>${prev}</h1> <h1>${curr.expenseName}</h1>`, '');

document.body.append(result);

about 4 years ago · Santiago Gelvez 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!