Skip to content

Instantly share code, notes, and snippets.

@eimon96
Created November 21, 2023 13:37
Show Gist options
  • Save eimon96/0ac3383b23003d8d0f8542a736b0e967 to your computer and use it in GitHub Desktop.
Save eimon96/0ac3383b23003d8d0f8542a736b0e967 to your computer and use it in GitHub Desktop.
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