如何从主实体获取 id 并将其包装到 normalizr 中的嵌套实体中

How to get id from main Entity and wrap it into nested one in normalizr

您好,我的包装对象规范化有问题。

我有一个仪表板和布局对象的数据数组,其中包含响应视图的断点。

想要将它们全部规范化为两个实体,我的意思是仪表板和布局。

{
   "dashboards":[
      {
         "id":1,
         "name":"First",
         "mode":"2",
         "layouts":{
            "lg":[
               {
                  "x":0,
                  "y":0,
                  "w":2,
                  "h":2,
                  "i":"sm1"
               },
               {
                  "x":2,
                  "y":0,
                  "w":2,
                  "h":2,
                  "i":"sm2"
               }
            ],
            "md":[
               {
                  "x":2,
                  "y":0,
                  "w":2,
                  "h":2,
                  "i":"sm2"
               }
            ]
         }
      }
   ]
}

我试着那样做。但是我无法从 Dashboards 获取密钥并将其放入 Layouts,因为它是一对一的关系。

const layouts = new schema.Entity('layouts');
const mode = new schema.Entity('modes');


const dashboards = new schema.Entity('dashboards', {
    layouts: layouts,
    mode: mode
});
const dashboardListSchema = new schema.Array(dashboards);

const normalizedData = normalize(response, dashboardListSchema);

我现在的输出是这样的:

仪表板:{“1”:{"id":1,"name":"Główny","mode":“2”}}

布局:{ "undefined":{ "lg":[ { "x":0,"y":0,"w":2,"h": 2, "i": "sm1" }, { "x": 2, "y": 0, "w": 2, "h": 2 , "i": "sm2" } ], "md": [ { "x": 2, "y": 0, "w": 2, "h": 2, "i": "sm2" } ] }

我想要仪表板的 id 而不是 undefined。谁能帮帮我?

这是我的解决方案:)

const layouts = new schema.Entity('layouts', {}, {
    idAttribute: (value, parent) => parent.id
});