如何使用normalizr标准化嵌套响应

时间:2019-04-22 21:45:07

标签: javascript reactjs redux normalizr

我想规范化我从API获得的嵌套JSON数据响应。数据响应包含属于多个班次的业务代表。实体之间的关系是多对多关联,因为,一个班次可以有许多代理,而一个代理可以属于许多班次。因此,我遇到了这个normalizr实用程序,在该实用程序中,我尝试将日期标准化为该表格,以便能够更轻松地解析它。

我试图将其标准化为

{
  entities : {
    shifts:[{...}],
    agents:[{...}]
  }
}
import { normalize, schema } from 'normalizr';
import stubData from './stubData';

const agent = new schema.Entity('agents', {});

const day_agents = new schema.Entity('day_agents', {
  agents: [agent]
});

const shift = new schema.Entity('shifts', {
  day_agents
});

const normalizedData = normalize(stubData, shift);

stubData:

"shifts": [{
        "id": 1,
        "name": "Shift 1",
        "start_time": "9:00",
        "end_time": "5:00",
        "day_agents": {
            "10/4/2019": {
                  "leaves": 1,
                  "agents": [
            {
                          "id": 1,
                          "name": "a",
                          "email": "a@b.co",
                          "group_id": 1,
                          "Leave": false
                      },
                      {
                          "id": 2,
                          "name": "b",
                          "email": "b@b.co",
                          "group_id": 1,
                          "Leave": false
                      }
                  ]
              },
            "11/4/2019": {
                "leaves": 1,
                "agents": [{
                        "id": 4,
                        "name": "c",
                        "email": "c@c.co",
                        "group_id": 2,
                        "Leave": true
                    },
                    {
                        "id": 5,
                        "name": "d",
                        "email": "d@d.co",
                        "group_id": 2,
                        "Leave": false
                    }
                ]
            }
    }
    }]

0 个答案:

没有答案