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
¿Cómo modificar una matriz de objetos con ciertos grupos en una matriz de objetos con grupos y elementos relacionados?

Tengo la siguiente matriz de objetos que quiero cambiar su estructura, estoy tratando de usar reduce para obtener un nuevo objeto con elementos que tienen el mismo grupo.

 const arr = [{id: 'c8c443', group: 'a', equipment: 'ball'}, {id: 'c84445', group: 'a', equipment: 'ball'}, {id: 'c8c655', group: 'b', equipment: 'basketball'}, {id: 'c8c634', group: 'b', equipment: 'basketball'}] const newArr = arr.reduce( (obj: any, value: any, i) => [ ...obj, { group: value.map((el: any) => el.group), items: value.map((el: any) => el.equipment), }, ], [] ); // Results [{group: 'a', items: ['ball','ball']}, {group: 'b', items: ['basketball','basketball']}]

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

0

Aquí hay un ejemplo:

 const arr = [{id: 'c8c443', group: 'a', equipment: 'ball'}, {id: 'c84445', group: 'a', equipment: 'ball'}, {id: 'c8c655', group: 'b', equipment: 'basketball'}, {id: 'c8c634', group: 'b', equipment: 'basketball'}] const newArr = arr.reduce((acc, item) => { if (acc.some(key => key.group === item.group)) { acc = acc.map(i => i.group === item.group ? {...i, items: [...i.items, item.equipment]} : i) } else { acc.push({ group: item.group, items: [item.equipment] }) } return acc }, []) console.log(newArr)

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!