【问题标题】:How is Normalizr used properly with 3 a merged Array of Objects?Normalizr 如何与 3 个合并的对象数组正确使用?
【发布时间】:2022-01-27 02:25:27
【问题描述】:

我正在尝试使用 Normalizr 规范化我的对象数组,但没有得到正确或预期的结果。

这些是 3 个对象数组:

const assistants = [
  {
    id: 1,
    first_name: "Nickolas",
    last_name: "Seamans",
    phone: "+62 949 597 4013",
    email: "nseamans0@dentistcompanybvt.com"
  },
  {
    id: 2,
    first_name: "Peri",
    last_name: "Helversen",
    phone: "+51 886 232 9275",
    email: "phelversen1@dentistcompanybvt.com"
  }
];

const clients = [
  {
    id: 1,
    first_name: "Mona",
    last_name: "Shakelade",
    phone: "+63 475 243 2059",
    email: "mshakelade0@sourceforge.net",
    date_of_birth: "26/01/1987",
    status: null
  },
  {
    id: 2,
    first_name: "Dario",
    last_name: "Aizikovitz",
    phone: "+33 454 959 7355",
    email: "daizikovitz1@buzzfeed.com",
    date_of_birth: "16/08/1999",
    status: null
  }
];

const dentists = [
  {
    id: 1,
    first_name: "Tessa",
    last_name: "Iiannone",
    phone: "+234 325 319 4277",
    email: "tiiannone0@dentistcompanybvt.com"
  },
  {
    id: 2,
    first_name: "Kennett",
    last_name: "Pedreschi",
    phone: "+48 204 144 9885",
    email: "kpedreschi1@dentistcompanybvt.com"
  }
];

然后我将它们与:

import assistants from "../data/assistants";
import dentists from "../data/dentists";
import clients from "../data/clients";

const users = {
  dentists: dentists,
  assistants: assistants,
  clients: clients
};

因此 console.log(users) 的结果是:

{dentists: Array(2), assistants: Array(2), clients: Array(2)}
assistants: (2) [{…}, {…}]
clients: (2) [{…}, {…}]
dentists: (2) [{…}, {…}]
[[Prototype]]: Object

我已经编造了这个 normalizr 步骤:

const dentistsList = new schema.Entity("dentists", {}, { idAttribute: "id" });
const dentistsSchema = [dentistsList];
const assistantsList = new schema.Entity(
  "assistants",
  {},
  { idAttribute: "id" }
);
const assistantsSchema = [assistantsList];
const clientsList = new schema.Entity("clients", {}, { idAttribute: "id" });
const clientsSchema = [clientsList];

const usersSchema = new schema.Entity("users", {
  clients: clientsSchema,
  assistants: assistantsSchema,
  dentists: dentistsSchema
});

const usersArraySchema = [usersSchema];

const normalizedArray = normalize(users, [usersArraySchema]);

现在我使用 console.log(normalizedArray) 的结果是:

{entities: {…}, result: Array(3)}
entities:
users: {1: {…}, 2: {…}}
[[Prototype]]: Object
result: Array(3)
0: (2) [1, 2]
1: (2) [1, 2]
2: (2) [1, 2]
length: 3
[[Prototype]]: Array(0)
[[Prototype]]: Object

那么我的助手和牙医去了哪里?但是,它们在“结果”部分中单独计算...

谁能帮帮我?

【问题讨论】:

    标签: reactjs redux normalization normalizr


    【解决方案1】:

    最终,解决方案并没有那么难,但一旦你知道了,情况总是如此。以下符号起到了作用:

    const assistant = new schema.Entity("assistants");
    const dentist = new schema.Entity("dentists");
    const client = new schema.Entity("clients");
    const userSchema = {
      assistants: [assistant],
      dentists: [dentist],
      clients: [client]
    };
    const normalizedUsers = normalize(users, userSchema);
    
    

    console.log(normalisedUsers) 的结果现在是:

    {entities: {…}, result: {…}}
    entities:
    assistants: {201: {…}, 202: {…}}
    clients: {1: {…}, 2: {…}}
    dentists: {101: {…}, 102: {…}}
    [[Prototype]]: Object
    result:
    assistants: (2) [201, 202]
    clients: (2) [1, 2]
    dentists: (2) [101, 102]
    [[Prototype]]: Object
    [[Prototype]]: Object
    

    我希望大家可以从我的解决方案分享中受益。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-17
      • 2019-08-31
      • 2021-02-10
      • 2020-09-16
      • 2018-07-05
      • 2018-11-26
      • 1970-01-01
      • 2018-09-29
      相关资源
      最近更新 更多