为什么在标准化状态形状中需要所有“ids”的数组?
Why is an array of all `ids` needed in a normalized state shape?
comments : {
byId : {
"comment1" : {
id : "comment1",
author : "user2",
comment : ".....",
},
"comment2" : {
id : "comment2",
author : "user3",
comment : ".....",
},
"comment3" : {
id : "comment3",
author : "user3",
comment : ".....",
},
"comment4" : {
id : "comment4",
author : "user1",
comment : ".....",
},
"comment5" : {
id : "comment5",
author : "user3",
comment : ".....",
},
},
allIds : ["comment1", "comment2", "comment3", "commment4", "comment5"]
}
在上面的例子中,我有什么理由需要包含它吗api
包含它。我假设这样你可以更快地进行计数,你可能可以排序但通常我不明白是否会影响性能。
这不是 Redux 要求的任何东西,这是 normalizr 的东西。为了回答您的问题,JavaScript 反对 can't be replied upon to retain sort order in certain situations。将 ID 放入数组中可以让您保留标准化之前存在的排序顺序。
:
As for the ID arrays, while JS engines now have a fairly standardized process for iterating across keys in an object, you shouldn't rely on that to define ordering. Storing arrays of IDs allows you to define an order for items.
comments : {
byId : {
"comment1" : {
id : "comment1",
author : "user2",
comment : ".....",
},
"comment2" : {
id : "comment2",
author : "user3",
comment : ".....",
},
"comment3" : {
id : "comment3",
author : "user3",
comment : ".....",
},
"comment4" : {
id : "comment4",
author : "user1",
comment : ".....",
},
"comment5" : {
id : "comment5",
author : "user3",
comment : ".....",
},
},
allIds : ["comment1", "comment2", "comment3", "commment4", "comment5"]
}
在上面的例子中,我有什么理由需要包含它吗api
包含它。我假设这样你可以更快地进行计数,你可能可以排序但通常我不明白是否会影响性能。
这不是 Redux 要求的任何东西,这是 normalizr 的东西。为了回答您的问题,JavaScript 反对 can't be replied upon to retain sort order in certain situations。将 ID 放入数组中可以让您保留标准化之前存在的排序顺序。
As for the ID arrays, while JS engines now have a fairly standardized process for iterating across keys in an object, you shouldn't rely on that to define ordering. Storing arrays of IDs allows you to define an order for items.