如何合并 normalizr 函数中的相似值?

how to merge similar values in normalizr function?

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

[
   {
      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 去平这个

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

但是这样做我得到一个错误 "When merging two people, found unequal data in their "“儿童”值。使用较早的值。” 我如何调整 normalizr 以合并具有相同 ID 的人(使用最新数据更新实体)?

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

对于 Normalizr@2:

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


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