【问题标题】:Normalizr usage for deep nested obj深度嵌套 obj 的 Normalizr 用法
【发布时间】:2019-07-31 13:09:29
【问题描述】:

我正在尝试使用 Normalizr 来规范化和非规范化我在我的 angular ngrx 应用程序中使用的对象。

我在这里找到了几乎解决方案:https://www.snip2code.com/Snippet/1028240/Deep-nested-tree-normalize-with-normaliz

import {normalize, Schema, arrayOf} from 'normalizr';

var data = [
    {
        "id": 1,
        "name": "О компании",
        "children": [
            {
                "id": 5,
                "name": "Руководство",
                "children": [
                    {
                        "id": 6,
                        "name": "Генеральный директор",
                        "children": [
                            {
                                "id": 20,
                                "name": "Зам гендира"
                            },
                            {
                                "id": 8,
                                "name": "Секретарша"
                            }
                        ]
                    },
                    {
                        "id": 7,
                        "name": "Главный бухгалтер",
                        "children": [
                            {
                                "id": 21,
                                "name": "Зам главбуха"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "id": 2,
        "name": "Вакансии",
        "children": [
            {
                "id": 9,
                "name": "Фронтенд-разработчик (JS)"
            },
            {
                "id": 10,
                "name": "Бэкэнд-разработчик (Java)"
            },
            {
                "id": 11,
                "name": "Оператор ЭВМ"
            }
        ]
    }
];

问题是以下解决方案中的许多功能发生了变化,我很难将相同的解决方案与更新版本的 normalizr 一起使用。也许你们中的一些人知道如何将以下解决方案重写为新的 normalizr?

var node = new Schema('nodes');
node.define({
    children: arrayOf(node)
});
var treeSchema = arrayOf(node);


var normalizedData = normalize(data, treeSchema);
console.log(normalizedData);

【问题讨论】:

    标签: json ngrx normalizr


    【解决方案1】:

    您正在查看一个使用过时版本的 Normalizr 的示例。尝试阅读 the docs 了解新 API

    【讨论】:

      【解决方案2】:

      我做到了:)

       const node = new schema.Entity('nodes');
          node.define({
              children: [node]
          })
          const treeSchema = [node];
          const normalizedData = normalize(this.form,treeSchema);
      

      【讨论】:

        猜你喜欢
        • 2021-05-22
        • 2012-08-29
        • 1970-01-01
        • 1970-01-01
        • 2016-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多