如何使用normalizr获取相关的一对多ID列表?

时间:2019-05-21 14:46:04

标签: reactjs redux normalizr

给出了这样的一组数据:

data = [
    {
    "id": 1,
    "name": "item 1",
    "description": "first item",
    "category": "a"
    },
    {
    "id": 2,
    "name": "item 2",
    "description": "second item",
    "category": "b"
    },
    {
    "id": 3,
    "name": "item 3",
    "description": "third item",
    "category": "c"
    },
    {
    "id": 4,
    "name": "item 4",
    "description": "forth item",
    "category": "a"
    },
]

我正在尝试将其标准化为以下形式:

items = {
    "1" :{
        "id": 1,
        "name": "item 1",
        "description": "first item",
        "category": "a"
    },
    "2": {
        "id": 2,
        "name": "item 2",
        "description": "second item",
        "category": "b"
    },
    "3" :{
        "id": 3,
        "name": "item 3",
        "description": "third item",
        "category": "c"
    },
    "4" : {
        "id": 4,
        "name": "item 4",
        "description": "forth item",
        "category": "a"
    },
},
categoryItems = {
    "a": [1,4],
    "b": [2]
    "c": [3]
}

我尝试过类似的事情:

const category = new schema.Entity('categoryItems');
const items = new schema.Entity('items',{
  category:[category]
});

const itemSchema = [items];
const nData = normalize(data, itemSchema);

以及一些显示processStrategy和mergeStrategy

的示例

但是我无处可寻...我回来的都是一样的老


"entities" : "items" = {
    "1" :{
        "id": 1,
        "name": "item 1",
        "description": "first item",
        "category": "a"
    },
    "2": {
        "id": 2,
        "name": "item 2",
        "description": "second item",
        "category": "b"
    },
    "3" :{
        "id": 3,
        "name": "item 3",
        "description": "third item",
        "category": "c"
    },
    "4" : {
        "id": 4,
        "name": "item 4",
        "description": "forth item",
        "category": "a"
    },
},

"results" :{blaa, blaa, blaa ....} //not literally :)

我想念什么? 我敢肯定,这很明显,而且我也没有感到沮丧

0 个答案:

没有答案