Created
November 21, 2023 13:37
-
-
Save eimon96/0ac3383b23003d8d0f8542a736b0e967 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
state = { peopleIds: [], people: {}, hobbies: {} } | |
people = [ | |
{ | |
id: 1, | |
name: 'Person 1', | |
hobbies: [ | |
{ | |
id: 11, | |
name: "Dark Souls", | |
type: "SinglePlayer" | |
}, | |
{ | |
id: 22, | |
name: "Call of Duty", | |
type: "Multiplayer" | |
} | |
] | |
}, | |
{ | |
id: 2, | |
name: 'Person 2', | |
hobbies: [ | |
{ | |
id: 11, | |
name: "Dark Souls", | |
type: "SinglePlayer" | |
}, | |
{ | |
id: 33, | |
name: "God of War", | |
type: "SinglePlayer" | |
} | |
] | |
} | |
] | |
people.map( person => { | |
state.peopleIds = [ | |
...state.peopleIds, | |
person.id | |
] | |
hobbies = [...person.hobbies] | |
state.people[person.id] = { | |
...person, | |
hobbies: hobbies.map( h => h.id ) | |
} | |
hobbies.map( hobby => { | |
state.hobbies[hobby.id] = { ...hobby } | |
}) | |
}) | |
console.dir(state, { depth: null }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment