【发布时间】: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