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

218
Views
how to combine an array with the same keyon react?

i have an output like this:

[
  { GolonganName: '3B - Niaga Besar', data: [ 172000 ] },
  { GolonganName: '3A - Niaga Kecil', data: [ 76700 ] },
  { GolonganName: '2B - Rumah Tangga Menengah', data: [ 57000 ] },
  { GolonganName: 'IA - Rumah Tangga', data: [ 272000 ] },
  { GolonganName: 'R5 - Instansi Pemerintah', data: [ 429300 ] },
  { GolonganName: '3B - Niaga Besar', data: [ 0 ] },
  { GolonganName: '3A - Niaga Kecil', data: [ 0 ] },
  { GolonganName: '2B - Rumah Tangga Menengah', data: [ 50200 ] },
  { GolonganName: 'IA - Rumah Tangga', data: [ 0 ] },
  { GolonganName: 'R5 - Instansi Pemerintah', data: [ 1467000 ] }
]

but i want to push my data with the same GolonganName into 1 data only like this:

[
  { GolonganName: '3B - Niaga Besar', data: [ 172000, 0 ] },
  { GolonganName: '3A - Niaga Kecil', data: [ 76700, 0 ] },
  { GolonganName: '2B - Rumah Tangga Menengah', data: [ 57000, 50200 ] },
  { GolonganName: 'IA - Rumah Tangga', data: [ 272000, 0 ] },
  { GolonganName: 'R5 - Instansi Pemerintah', data: [ 429300, 1467000 ] },
]
 

i've tried using array.map but its not working properly

 let dataMap: any = seriesGolongan.map((item: any, i: any) => {
    const length = responseBillingTarget.length;
    let lenGol = responseGetGolongan.length;
    for (let index = 0; index < responseGetGolongan.length; index++) {
      const element = responseGetGolongan[index];
      if (element.name == item.GolonganName) {
        if (length == 2) {
          resultGol.push({
            GolonganName: item.GolonganName,
            data: [parseInt(item.data)],
          });
        }
      }
    }
  });

how to solve it? can u guys help me pls ?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use grouping by hash approach:

const inputData = [{GolonganName:'3B - Niaga Besar',data:[172000]},{GolonganName:'3A - Niaga Kecil',data:[76700]},{GolonganName:'2B - Rumah Tangga Menengah',data:[57000]},{GolonganName:'IA - Rumah Tangga',data:[272000]},{GolonganName:'R5 - Instansi Pemerintah',data:[429300]},{GolonganName:'3B - Niaga Besar',data:[0]},{GolonganName:'3A - Niaga Kecil',data:[0]},{GolonganName:'2B - Rumah Tangga Menengah',data:[50200]},{GolonganName:'IA - Rumah Tangga',data:[0]},{GolonganName:'R5 - Instansi Pemerintah',data:[1467000]}];

const groups = inputData.reduce((acc, { GolonganName, data }) => {
    acc[GolonganName] ??= { GolonganName, data: [] };
    acc[GolonganName].data.push(...data);
    return acc;
}, {});

const result = Object.values(groups);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }

about 4 years ago · Santiago Trujillo 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!