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

138
Views
javascript cómo agrupar y combinar una sola matriz de objetos

Tengo una matriz de objetos a continuación,

 [ { locationCode: 'GMP', Qr: 46 }, { locationCode: 'CMT', Qr: 35 }, { locationCode: 'GMP', Cash: 29 }, { locationCode: 'CMT', Cash: 26 }, { locationCode: 'CMS', Qr: 22 }, { locationCode: 'CMT', Voucher: 6 }, { locationCode: 'CMS', Cash: 2 } ]

Quiero agrupar el código de locationCode y combinar otros en un objeto como este

 [ { locationCode: 'GMP', Qr: 46, Cash: 29 }, { locationCode: 'CMT', Qr: 35, Cash: 26, Voucher: 6 }, { locationCode: 'CMS', Qr: 22, Cash: 2 } ]

¿Alguien puede explicar eso con Javascript o lodash? ¡Gracias!

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

0

Puede lograr fácilmente este resultado usando Map

 const arr = [ { locationCode: "GMP", Qr: 46 }, { locationCode: "CMT", Qr: 35 }, { locationCode: "GMP", Cash: 29 }, { locationCode: "CMT", Cash: 26 }, { locationCode: "CMS", Qr: 22 }, { locationCode: "CMT", Voucher: 6 }, { locationCode: "CMS", Cash: 2 }, ]; const map = new Map(); arr.forEach((o) => map.set(o.locationCode, { ...(map.get(o.locationCode) ?? {}), ...o }) ); const result = [...map.values()]; console.log(result);
 /* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */ .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar Array.reduce para crear un objeto con clave de locationCode , también usaríamos la destructuring de objetos para ensamblar el objeto en cada valor clave.

 const input = [ { locationCode: 'GMP', Qr: 46 }, { locationCode: 'CMT', Qr: 35 }, { locationCode: 'GMP', Cash: 29 }, { locationCode: 'CMT', Cash: 26 }, { locationCode: 'CMS', Qr: 22 }, { locationCode: 'CMT', Voucher: 6 }, { locationCode: 'CMS', Cash: 2 } ] const result = Object.values(input.reduce((acc, cur) => { acc[cur.locationCode] = acc[cur.locationCode] || { locationCode: cur.locationCode }; acc[cur.locationCode] = { ...acc[cur.locationCode], ...cur }; return acc; }, {})) console.log('Result:', result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

import React, { PureComponent } from 'react' class App extends PureComponent { constructor(props) { super(props); this.state = { data: [ {locationCode: 'GMP', Qr: 46}, {locationCode: 'CMT', Qr: 35}, {locationCode: 'GMP', Cash: 29}, {locationCode: 'CMT', Cash: 26}, {locationCode: 'CMS', Qr: 22}, {locationCode: 'CMT', Voucher: 6}, {locationCode: 'CMS', Cash: 2}, ], }; } render() { return ( <div> {this.state.data.map((item, i) => { console.log(item) return ( <React.Fragment key={i}> <h3>{item.locationCode}</h3> </React.Fragment> ); })} </div> ) } } export default App
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!