我有一个嵌套的对象数组,如下所示:
var posts = [
{
_id:1234,
body:"text",
comments:[
{
_id:234,
body:"hello world", {
]
},
{
_id:434,
body:"hello world",
replies:[
{
_id:0e2345,
body:"hello",
{
]
}
]
}
]
我想使用 normalizr 来简化数组并与 redux 一起使用。我已经阅读了 Normalizr 文档,但它的例子很少,我不知道我做错了什么。
我尝试了以下代码但没有成功。我得到的结果是一个未定义的数组。
export function getPosts(state, action) {
const { payload } = action;
const { data} = payload;
const normalized = new schema.Entity("posts", {}, { idAttribute: "_id",});
const normalizedData = normalize(data, [normalized]);
return {
...state,
normalizedData,
};
}
我需要这样的东西:
entities:{
posts:{
123:{
_id:123,
body:"hello world",
comments:{
234:{
_id:234,
body:"hello world",
replies:{
0e2345:{
_id:0e2345,
body:"oh no"
}
}
}
}
}
}
}