I'm making an API request and running the data through groupBy and sortBy to get a slightly more structured object. Then I save it to the state.
useEffect(() => {
const fetchTeamData = async () => {
const result = await axios(
`https://example.com/api-call`,
).then((result) => {
let teams = _.groupBy(_.sortBy(result.data.results, "season"), "player_profile.team");
setTeamInfo(teams);
});
};
fetchTeamAffinityData();
}, []);
This gives me an object similar to:
{
Angels: [
0: {
player_profile:{
name: "John Doe",
number: "10"
},
season: 1
},
1: {
player_profile:{
name: "Mike Trout",
number: "21"
},
season: 2
}
],
Diamondbacks: [
0: {
player_profile:{
name: "Randy Johnson",
number: "51"
},
season: 1
},
1: {
player_profile:{
name: "Brandon Webb",
number: "16"
},
season: 2
}
],
}
This is the output I'm looking for:
Team: Angels
Player 1: John Doe
Player 1 Number: 10
Player 2: Mike Trout
Player 2 Number: 21
Team: Diamondbacks
Player 1: Randy Johnson
Player 1 Number: 51
Player 2: Bradon Webb
Player 2 Number: 17
Here is what I've tried, which gives me a loop of the team names. But since I'm mapping the object keys, I lose the actual data for the nested object.
{Object.keys(teamAffinityInfo?? "").map((team, index) => (
Team: {team}
))}
How can I work with deeply nested objects in a sensible way?
Object.entries? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…