I am working with an array of actors, each of whom have arrays such as movies
, awards
, and more. I need to restructure the data received from the server into a tree format. The name of each array should be placed in a new array called children
, which includes 3 properties: a unique id
, the array name (e.g., 'movies'), and the actual data within that array. I require assistance in dynamically creating this structure to accommodate potential data expansion. Any guidance would be greatly appreciated.
Here is the initial data structure from the server:
[{
name: 'Tom Holland',
id: 1,
movies: [{
typename: 'MoviesData',
id: 12,
name: 'Spider-Man NWH',
stars: 4
}, {
typename: 'MoviesData',
id: 13,
name: 'Spider-Man Far from Home',
stars: 4
}],
awards: [{
typename: 'AwardsData',
id: 6,
name: 'BAFTA Award',
year: 2017
}, {
typename: 'AwardsData',
id: 7,
name: 'Empire Award',
year: 2013
}]
//...additional arrays like Hobbies, TV-Shows, etc.
}
//...more Actors
]
This is the desired tree data structure:
[{
name: "Tom Holland",
id: 1,
children: [{
id: 3, // generated id for each array
name: "Movies",
children: [
{typename: 'MoviesData', id: 12, name: 'Spider-Man NWH', stars: 4},
{typename: 'MoviesData', id: 13, name: 'Spider-Man Far from Home', stars: 4}
// additional movies...
]
}, {
id: 15, // generated id
name: "Awards",
children: [
{typename:'AwardsData', id: 6, name: 'BAFTA Award', year: 2017},
{typename:'AwardsData', id: 7, name: 'Empire Award', value: 2013}
// additional awards...
]
}, //...]
}, //...]