未使用normalizr进行标准化的对象数组
我正在尝试规范收到的响应。我能够按照外部对象的groupId规范化数据,但是无法对包含对象及其属性的管理员列表的外部对象内部的数组执行相同的操作。我想要的是根据列表的ID将列表转换为对象,但根据对象的索引来区分对象。
schema.js
import { schema } from 'normalizr';
const groupAdministrator = new schema.Entity('groupAdministratorList' , {groups: {}}, { idAttribute: 'groupAdministratorId'});
export const groupSchema = {
groupAdministratorList: { groupAdministratorId: groupAdministrator}
};
export const groupsSchema = new schema.Entity(
'groups',
groupSchema,
{ idAttribute: 'groupId' }
);
reducer.js
import { normalize } from 'normalizr';
import { groupsSchema } from "./schema";
const { entities } = normalize(action.payload, groupsSchema);
if (!entities.groups) return state;
const newState = { groups: entities.groups };
return merge(state, newState, { deep: true });
以下是我从API得到的响应:
{
groupId: "uniqueId"
groupName: "someName"
groupAdministratorList: [
{
groupAdministratorId: "uniqueId1",
role: "someRole"
},
{
groupAdministratorId: "uniqueId2",
role: "someRole"
}
]
}
以下是归一化后的结果:
uniqueId: {
groupId: "uniqueId"
groupName: "someName"
groupAdministratorList: {
0: {
groupAdministratorId: "uniqueId1",
role: "someRole"
},
1: {
groupAdministratorId: "uniqueId2",
role: "someRole"
}
}
}
以下是我真正想要的:
uniqueId: {
groupId: "uniqueId"
groupName: "someName"
groupAdministratorList: {
uniqueId1: {
groupAdministratorId: "uniqueId1",
role: "someRole"
},
uniqueId2: {
groupAdministratorId: "uniqueId2",
role: "someRole"
}
}
}