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