如何正确使用Normerizr和3个合并的对象数组? [英] How is Normalizr used properly with 3 a merged Array of Objects?
本文介绍了如何正确使用Normerizr和3个合并的对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Normal izr正常化我的对象数组,但没有得到正确或预期的结果。
以下是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(用户)的结果是:
{dentists: Array(2), assistants: Array(2), clients: Array(2)}
assistants: (2) [{…}, {…}]
clients: (2) [{…}, {…}]
dentists: (2) [{…}, {…}]
[[Prototype]]: Object
我编造了以下标准化步骤:
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(NormizedArray)的结果是:
{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
那么我的助手和牙医都去哪里了?但是,它们在‘Result’部分单独计算...
有人能帮我吗?
推荐答案
最终,解决方案并不是那么难,但一旦您知道了,情况总是如此。以下表示法起到了作用:
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(NormisedUser)的结果现在为:
{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
我希望大家能从我的解决方案分享中受益。
这篇关于如何正确使用Normerizr和3个合并的对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文