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

172
Views
How can I merge an array and group by key in Javascript?

I have multiple array in reactjs.

{last_name: 'User1', status: 'BRONZE', type: 'Maintenance', due_date: '2022-06-04 00:00:00'}
{last_name: 'User1', status: 'BRONZE', type: 'Contrôle technique', due_date: '2022-06-18 00:00:00'}
{last_name: 'User2', status: 'BRONZE', type: 'Unknow', due_date: null}

I would like to merge the array by user last_name to have a result like this:

{last_name: 'User1', status1: 'BRONZE', type: 'Maintenance1', due_date1: '2022-06-04 00:00:00', status2: 'BRONZE', type2: 'Contrôle technique', due_date: '2022-06-18 00:00:00'}
{last_name: 'User2', status: 'BRONZE', type: 'Unknow', due_date: null}

In my example I have merge the array 1 and 2 to have 1 array "group by" last_name, here User1 but I need to keep the value of the second array too.

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

0

Your question lacks a few details. Are status and due_date the only fields that might be repeated? If so, the below answer should work. If not, you might want to specify which keys should be merged with an index in the field name, and which can be pulled in as is.

I'm not sure why you would want to structure your data this way-- having different field names seems like it would make it difficult to find the data, but leaving that aside:

const data = [{
  last_name: 'User1',
  status: 'BRONZE',
  type: 'Maintenance',
  due_date: '2022-06-04 00:00:00'
}, {
  last_name: 'User1',
  status: 'BRONZE',
  type: 'Contrôle technique',
  due_date: '2022-06-18 00:00:00'
}, {
  last_name: 'User2',
  status: 'BRONZE',
  type: 'Unknow',
  due_date: null
}]

const mergedMap = {}

// Group list elements by last_name
for (const el of data) {
  if (el.last_name in mergedMap) {
    mergedMap[el.last_name].push(el)
  } else {
    mergedMap[el.last_name] = [el]
  }
}

// Iterate over "user" groups, modifying field names.
const mergedList = []
for (const last_name in mergedMap) {
  const elCount = mergedMap[last_name].length
  // If there's only one entry for this "last_name", keep it as is, 
  // then continue to next user.
  if (elCount === 1){
    mergedList.push(mergedMap[last_name][0])
    continue
  }
  const mergedUser = mergedMap[last_name].reduce((merged, el, index) => ({
    // Keep whatever keys are already here
    ...merged,
    // last_name and status are assumed to always be the same
    // for a given user, so they're safe to overwrite each time
    last_name: el.last_name,
    status: el.status,
    // type and due_date might be unique for each entry, so
    // we add an index to the field name and copy the new value in
    [`type${index + 1}`]: el.type,
    [`due_date${index + 1}`]: el.due_date,
  }), {})
  mergedList.push(mergedUser)
}

console.log(JSON.stringify(mergedList, null, 2))

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!