【问题标题】:how to merge similar values in normalizr function?如何在 normalizr 函数中合并相似的值?
【发布时间】:2016-10-14 03:16:36
【问题描述】:

我收到来自服务器的异常响应

[
   {
      id: 1,
      name: "Alexandr",
      children: [
         {
            id: 2,
            name: "Stephan"
         },
         {
            id: 3,
            name: "Nick"
         }
      ]
   },
   {
      id: 4,
      name: "David",
      children: [
         {
            id: 3,
            name: "Nick"
         },
         {
            id: 6,
            name: "Paul"
         }
      ]
   }
]

我想规范化此响应以接收与所有人的措辞。所以,我用 normalizr go flat this

const people= new Schema('people');
people.define({
    Children: arrayOf(people),
    NotOwnChildren: arrayOf(people)
});
let normalized = normalize(response.data, arrayOf(people));

但是这样做我得到一个错误 “合并两个人时,发现他们的“儿童”值中的数据不相等。使用较早的值。 如何调整 normalizr 以合并具有相同 ID 的人(使用最新数据更新实体)?

【问题讨论】:

    标签: reactjs redux react-redux normalizr


    【解决方案1】:

    您似乎收到了两个 people,其中一个键的值不同(我假设您的示例输入被截断)。

    对于 Normalizr@2:

    您可以使用自定义mergeIntoEntity 函数手动解决问题。


    对于 Normalizr@>=3.0.0,您需要使用 mergeStrategy

    【讨论】:

    • 感谢您的回答
    猜你喜欢
    • 2023-02-22
    • 2021-11-10
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 2012-04-18
    • 1970-01-01
    • 2023-02-01
    相关资源
    最近更新 更多