I am facing a challenge with a JavaScript array structure that contains three top-level objects. Each of these objects has an "teamLineup" array, which in turn holds two more objects named "team". These "team" objects have another array called "starters", where the player names are nested under the key "name". The structure of the top-level objects is as follows:
let game1 = {
teamLineup: [{
team: {
actual: {
starters: [{
player: { name: 'Joe' },
player: { name: 'Kim' }
}]
}
}
},{
team: {
actual: {
starters: [{
player: { name: 'John' },
player: { name: 'Shauna' }
}]
}
}
}]
}
(game2....game3)
My objective is to simplify this original array into a single array containing only the player names presented as strings.
let games = [game1, game2, game3]
let allStartingPlayers = games.map(a => a.teamLineup.map( b => b.team.actual.starters.map(c => c.player.name)))
console.log(allStartingPlayers);
An example output could look like this:
[ [ [ 'Kim' ], [ 'Shauna' ] ],
[ [ 'Nicole' ], [ 'Jennifer' ] ],
[ [ 'Sandy' ], [ 'David' ] ] ]
This code currently encounters two issues. It only retrieves the second player's name from each "starters" array and generates three nested arrays instead of a flat structure.