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

100
Views
Array object function and trasform

i need to trasform this

const persons = [
  {id:7 ,firstname : "Malcom", lastname: "Reynolds"},
  {id:7, firstname : "Malcom", lastname: "Frye"},
  {id:2,firstname : "Jayne", lastname: "Cobb"}
];

in

const persons_ = [
  {id:7 ,
   firstname : "Malcom",
   item: [{lastname: "Reynolds"},{lastname: "Frye"}]
  } ,
  {id:2,
  firstname : "Jayne", 
  item:[{lastname: "Cobb"}]
  }
];

(grouping by id) can you help me? tks

peraphs i resolve with this code:

     const copyItems = []
persons.forEach(function(item_){
  const found = copyItems.find(element => element.id == item_.id);
  if (!(found)){
    copyItems.push( {id:item_.id,firstname:item_.firstname,item:[{lastname: item_.lastname}]}    )
   }else
   {
    var item ={lastname: item_.lastname}
    copyItems[copyItems.length-1].item.push(item)
   }
})

but i suppose there is a better way.

about 4 years ago · Juan Pablo Isaza
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!