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

258
Views
How to convert data1 to like data2
data1 = [{name:"sonu",roll:34,file:"asj.jpg"},  {name:"sonu",roll:34,file:"asj1.jpg"},{name:"sonu",roll:34,file:"asj2.jpg"},{name:"dip",roll:67,file:"fgd3.jpg"},
    {name:"dip",roll:67,file:"fgd4.jpg"},
    {name:"dip",roll:67,file:"fgd5.jpg"}] 

"data" json convert to like "data2"

 data2= [{name:"sonu",roll:34,file:[asj.jpg,asj1.jpg,asj2.jpg]},
{name:"dip",roll:67,file:[fgd3.jpg,fgd.jpg,fgd5.jpg]}]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use Array.reduce() to reduce the first array into what you want:

const data = [
    { name: "sonu", roll: 34, file: "asj.jpg" },
    { name: "sonu", roll: 34, file: "asj1.jpg" },
    { name: "sonu", roll: 34, file: "asj2.jpg" },
    { name: "dip", roll: 67, file: "fgd3.jpg" },
    { name: "dip", roll: 67, file: "fgd4.jpg" },
    { name: "dip", roll: 67, file: "fgd5.jpg" }
]

const data2 = data.reduce((prev, curr) => {
    const found = prev.find(o => o.name === curr.name)
    found ?
        found.file.push(curr.file)
        : prev.push({ ...curr, file: [curr.file] })

    return prev
}, [])


console.log(data2);

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!