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

158
Views
Error using for loop (objects inside array)

I am trying to populate a table with an array (products). My data works however it's currently putting all of the information in one row:

const products = [{
            username: JSON.stringify(data, ['from']),
            content: JSON.stringify(data, ['content']),
            date: JSON.stringify(data, ['updatedAt'])
            }]

However, I'm trying to create a for loop to make a new object for every data item. I thought I had the right concept but this isn't working at all:

const products = [
            for (let i = 0; i < data.length; i++) {
            username: JSON.stringify(data[i], ['from']),
            content: JSON.stringify(data[i], ['content']),
            date: JSON.stringify(data[i], ['updatedAt'])
            }]

Can anyone point me in the right direction?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can simply use map.

const products = data.map(item => ({
                username: JSON.stringify(item, ['from']),
                content: JSON.stringify(item, ['content']),
                date: JSON.stringify(item, ['updatedAt'])
               })) 

or using for loop.

let products = [];
            for (let i = 0; i < data.length; i++) {
                products.push({
                username: JSON.stringify(data[i], ['from']),
                content: JSON.stringify(data[i], ['content']),
                date: JSON.stringify(data[i], ['updatedAt'])
                }) 
            }

You can avoid stringify if not needed.


const products = data.map(item => ({
                username: item.from,
                content: item.content,
                date: item.updatedAt,
               })) 
about 4 years ago · Juan Pablo Isaza 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!