为什么在标准化状态形状中需要所有“ID”的数组?

时间:2017-09-08 21:50:19

标签: normalizr

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包括在内。我假设这样你可以更快地计算,你可以排序,但一般我不理解是否有性能损失。

1 个答案:

答案 0 :(得分:8)

这不是Redux所要求的任何东西,这是一个标准化的东西。要回答您的问题,JavaScript对象can't be replied upon to retain sort order in certain situations。将ID放在数组中可以保留规范化之前存在的排序顺序。

Quote from co-maintainer of Redux and author of "normalizing state shape section" of Redux docs

  

对于ID数组,虽然JS引擎现在有一个相当标准化的过程来迭代对象中的键,但您不应该依赖它来定义排序。存储ID数组允许您定义项目的顺序。