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

180
Views
combine array of object if atleast one property is common

//this one is actual array

 const data = [
        {
          name: 'shanu',
          label: 'ak',
          value: 1,
        },
        {
          name: 'shanu',
          label: 'pk',
          value: 2,
        },
        {
          name: 'bhanu',
          label: 'tk',
          value: 3,
        },
      ];
>

//and this  is the array that I want
let outPut =
[
{
name:'shanu',
label:['ak','pk'],
value:[1,2]
},
{
name:'bhanu',
label:['tk'],
value:[3]
}
]

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

0

You can use Array.prototype.reduce() like this:

const data = [
  {
    name: 'shanu',
    label: 'ak',
    value: 1,
  },
  {
    name: 'shanu',
    label: 'pk',
    value: 2,
  },
  {
    name: 'bhanu',
    label: 'tk',
    value: 3,
  },
];

const output = data.reduce((prev, curr) => {
  const tmp = prev.find((e) => e.name === curr.name)
  if (tmp) {
    tmp.label.push(curr.label)
    tmp.value.push(curr.value)
  } else {
    prev.push({
      name: curr.name,
      label: [curr.label],
      value: [curr.value],
    })
  }
  return prev
}, [])

console.log(output)

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!