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

186
Views
How to flatten an array of objects?

I have some data in a non desirable format and I would like to flatten it.

Data:

[
    {
        team: "Team 1",
        name: "John"
    },
    {
        team: "Team 1",
        name: "Stacy"
    },
    {
        team: "Team 1",
        name: "Jason"
    },
    {
        team: "Team 2",
        name: "Tim"
    },
    {
        team: "Team 2",
        name: "Andrew"
    },
    {
        team: "Team 2",
        name: "Steve"
    }
    ,
    {
        team: "Team 3",
        name: "Eric"
    },
    {
        team: "Team 3",
        name: "Frank"
    },
    {
        team: "Team 3",
        name: "Cory"
    }
]

The desired result is:

[
    {
        team: "Team 1",
        name: ["John", "Stacy", "Jason"],
        count: 3
    },
    {
        team: "Team 2",
        name: ["Tim", "Andrew", "Steve"],
        count: 3
    },
    {
        team: "Team 3",
        name: ["Eric", "Frank", "Cory"],
        count: 3
    }
]

I've tried looping through it and using Object.assing but that seemed the be the incorrect approach. Any suggestions on a good approach to flatted this data? Thanks

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

0

You can use Array.reduce.

In the reducer function, check whether the accumulator contains an item with the same team property. If so, increment its's count property and push the current item's name property to it's name property.

const arr=[{team:"Team 1",name:"John"},{team:"Team 1",name:"Stacy"},{team:"Team 1",name:"Jason"},{team:"Team 2",name:"Tim"},{team:"Team 2",name:"Andrew"},{team:"Team 2",name:"Steve"},{team:"Team 3",name:"Eric"},{team:"Team 3",name:"Frank"},{team:"Team 3",name:"Cory"}];

const res = arr.reduce((a,b) => {
  let itm = a.find(e => e.team == b.team);
  if(itm){
    itm.count++;
    itm.name.push(b.name);
  }else{
    a.push({team: b.team, name: [b.name], count:1})
  }
  return a;
}, [])

console.log(res)

A more efficient solution:

const arr=[{team:"Team 1",name:"John"},{team:"Team 1",name:"Stacy"},{team:"Team 1",name:"Jason"},{team:"Team 2",name:"Tim"},{team:"Team 2",name:"Andrew"},{team:"Team 2",name:"Steve"},{team:"Team 3",name:"Eric"},{team:"Team 3",name:"Frank"},{team:"Team 3",name:"Cory"}];

const obj = arr.reduce((a,b) => {
  let itm = a[b.team]
  if(itm){
    itm.count++;
    itm.name.push(b.name);
  }else{
    a[b.team] = {team: b.team, name: [b.name], count: 0}
  }
  return a;
}, {})

const res = Object.keys(obj).map(e => obj[e])

console.log(res)

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!