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

104
Views
Función de objeto de matriz y transformación

necesito transformar esto

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

en

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

(agrupando por id) ¿pueden ayudarme? gracias

quizás resuelvo con este código:

 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) } })

pero supongo que hay una mejor manera.

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!