I'm currently working with an array of objects that I need to restructure into a new array. Despite my efforts, I haven't quite achieved the desired outcome.
The initial data I have is organized as follows:
data = Team > Days > Games
This data consists of sports teams and the games they are scheduled to play on each day from Monday to Sunday (0-6).
I require the data to be transformed into this structure:
data = Days > Teams > Games
Here's an example of the original data:
var data = [
{
id: 1,
team: 'LA Lakers',
days: [
{ id: 0, name: 'Monday', games: [{ id: 1, home: false }, { id: 2, home: true }, { id: 3, home: true }] },
{ id: 1, name: 'Tuesday', games: [] },
{ id: 2, name: 'Wednesday', games: [] },
...
]
},
{
id: 2,
team: 'NJ Nets',
days: [
{ id: 0, name: 'Monday', games: [] },
{ id: 1, name: 'Tuesday', games: [] },
{ id: 2, name: 'Wednesday', games: [{ id: 1, home: false }, { id: 2, home: true }, { id: 3, home: true }] },
...
]
},
];
Here is the format I aim to achieve:
var newData = [
{
id: 0,
name: 'Monday',
teams: [
{
id: 1,
name: 'LA Lakers',
games: [{ id: 1, home: false }, { id: 2, home: true }, { id: 3, home: true }]
}
]
},
{ id: 1, name: 'Tuesday', teams: [] },
{
id: 2,
name: 'Wednesday',
teams: [
{
id: 2,
name: 'NJ Nets',
games: [{ ... }, { ... }, { ... }]
}
]
},
...
];
I am considering using lodash and exploring the groupBy feature or potentially utilizing a for loop to populate a new array. Any suggestions or insights would be greatly appreciated.