gpt4 book ai didi

javascript - normalizr 模式中的嵌套数组

转载 作者:搜寻专家 更新时间:2023-11-01 05:20:41 25 4
gpt4 key购买 nike

我需要规范化这些数据,以便我同时拥有一组列表和另一个待办事项。

const data = [ 
{
_id: '1',
title: 'List1',
todos: [
{
_id: "11",
text: "Test1"
}
]
},
{
_id: '2',
title: 'List2',
todos: [
{
_id: "22",
text: "Test2"
}
]
}
];

这是我得到的:

const todo = new schema.Entity('todos',{},{ idAttribute: '_id'});
const list = new schema.Entity('lists',{todos:todo},{idAttribute: '_id'});
const normalizedData = normalize(data, list);
console.log(normalizedData);

我一直在尝试他们的示例,但它们似乎都不适用于此数据。

如有任何帮助,我们将不胜感激。

最佳答案

您需要告诉架构todostodo 的数组并且您的输入数据是一个数组:

const list = new schema.Entity('lists', { todos: [ todo ]}, { idAttribute: '_id' });
const normalizedData = normalize(data, [ list ]);

const list = new schema.Entity('lists', { todos: new schema.Array(todo) } , { idAttribute: '_id' });
const normalizedData = normalize(data, new schema.Array(list));

关于javascript - normalizr 模式中的嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42028270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com