Skip to content

Instantly share code, notes, and snippets.

@baptistemanson
Last active July 20, 2017 20:38
Show Gist options
  • Save baptistemanson/acf84419d029ba9852c3397ebbc73b05 to your computer and use it in GitHub Desktop.
Save baptistemanson/acf84419d029ba9852c3397ebbc73b05 to your computer and use it in GitHub Desktop.
// 1 - instead of
const state = {
users: [
{ id: 1, name: "Bat", groups: [{ name: "admin" }, { name: "regular" }] },
{ id: 2, name: "Vince", groups: { name: "admin" } },
{ id: 1, name: "Romain", groups: { name: "normal" } }
]
};
// 2- Normalize and index by primary key
const state = {
users: {
1: { id: 1, name: "Bat", groups: [1, 2] },
2: { id: 2, name: "Vince", groups: [1] },
3: { id: 3, name: "Romain", groups: [2] }
},
groups: {
1: { id: 1, name: "admin" },
2: { id: 2, name: "regular" }
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment