【发布时间】:2018-04-27 19:38:27
【问题描述】:
我有一个从我的 API 返回的 JSON。
[{
"_id": "id1",
"userId": "u1",
"createdOn": "2017-11-13T19:24:05.269Z"
}, {
"_id": "id2",
"userId": "u2",
"createdOn": "2017-11-13T19:23:59.777Z"
}, {
"_id": "id3",
"userId": "u3",
"createdOn": "2017-11-09T17:34:14.507Z"
}, {
"_id": "id4",
"userId": "u4",
"createdOn": "2017-11-08T18:15:54.303Z"
}
]
首先,我需要拆分 createdOn(如 createdOn.split("T")[0])并设置一个包含仅日期字符串的新属性 createdDate。然后我想根据新属性 createdDate 对其进行分类,然后将它们分组为在同一日期创建的项目。所以我试图得到一个结构如下。
{
"history": {
"2017-11-08": {
"id4": {
"_id": "id4",
"userId": "u4",
"createdOn": "2017-11-08T18:15:54.303Z",
"createdDate": "2017-11-08"
}
},
"2017-11-09": {
"id3": {
"_id": "id3",
"userId": "u3",
"createdOn": "2017-11-09T17:34:14.507Z",
"createdDate": "2017-11-09"
}
},
"2017-11-13": {
"id1": {
"_id": "id1",
"userId": "u1",
"createdOn": "2017-11-13T19:24:05.269Z",
"createdDate": "2017-11-13"
},
"id2": {
"_id": "id2",
"userId": "u2",
"createdOn": "2017-11-13T19:23:59.777Z",
"createdDate": "2017-11-13"
}
}
}
}
我尝试了使用“processStrategy”(用于分割日期时间字符串)以及多个模式的不同选项
export const historySchema = new schema.Entity(
"history",
{},
{ idAttribute: "_id" },
);
export const historyGroupingSchema = new schema.Entity(
"history",
{ el: [historySchema] },
{ idAttribute: "createdDate" },
);
normalize(history, [
historyGroupingSchema,
])
但不起作用。如何使用normalizr 执行此操作。
【问题讨论】:
标签: json react-redux normalizr