如何将平面多分支数据转换为分层 JSON? [英] How to convert flat multi-branch data to hierarchical JSON?

查看:22
本文介绍了如何将平面多分支数据转换为分层 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<预><代码>[{"id": "a","pid": "a","name": "AA",},{"id": "b","pid": "a","name": "BB",},{"id": "c","pid": "a","name": "CC",},{"id": "x","pid": "b","name": "XX",}]

以上是我从数据库中得到的数据.每个人都有一个id 和一个pidpid 指向这个人的更高级别的人的id.如果一个人的级别最高,则 id 等于 pid.

我想将原始数据转换为分层 JSON,如下所示:

<预><代码>[{"id": "a","name": "AA",孩子": [{"id": "b",名称":BB"孩子": [{"id": "x",姓名":XX"}]},{"id": "c",名称":CC"}]}]

我使用的是 Node.js.

解决方案

我建议你创建一棵树,并以 id === pid 作为树的根,这适用于未排序的数据.

<块引用>

工作原理:

基本上,对于数组中的每个对象,构建新对象的 id 作为新对象的 parentid.

例如:

{ "id": 6, "pid": 4 }

它首先用 id 生成这个属性:

"6": {身份证":6,pid":4}

然后用 pid:

"4": {孩子们": [{身份证":6,pid":4}]},

虽然所有对象都被类似地处理,但我们最终得到了一棵树.

如果id === pid,则找到根节点.这是后面返回的对象.

var data = [{ "id": "f", "pid": "b", "name": "F" },{ "id": "e", "pid": "c", "name": "E" },{ "id": "d", "pid": "c", "name": "D" },{ "id": "c", "pid": "b", "name": "C" },{ "id": "a", "pid": "a", "name": "A" },{ "id": "b", "pid": "a", "name": "B" }],树 = 函数(数据){var r, o = Object.create(null);data.forEach(函数(一){a.children = o[a.id] &&o[a.id].儿童;o[a.id] = a;如果(a.id === a.pid){r = a;} 别的 {o[a.pid] = o[a.pid] ||{};o[a.pid].children = o[a.pid].children ||[];o[a.pid].children.push(a);}});返回 r;}(数据);console.log(tree);

[
  {
    "id": "a",
    "pid": "a",
    "name": "AA",
  },
  {
    "id": "b",
    "pid": "a",
    "name": "BB",
  },
  {
    "id": "c",
    "pid": "a",
    "name": "CC",
  },
  {
    "id": "x",
    "pid": "b",
    "name": "XX",
  }
]

Above is the data I got from the database. Every person has an id and a pid, pid points to the person's higher level person's id. If a person has highest level, the id equals pid.

I want to convert the raw data to hierarchical JSON, like this:

[
  {
    "id": "a",
    "name": "AA",
    "child": [
      {
        "id": "b",
        "name": "BB"
        "child": [
          {
            "id": "x",
            "name": "XX"
          }
        ]
      },
      {
        "id": "c",
        "name": "CC"
      }
    ]  
  }
]

I'm using Node.js.

解决方案

I suggest you to create a tree and take id === pid as a root for the tree, which works for unsorted data.

How it works:

Basically, for every object in the array, it takes the id for building a new object as parentid for a new object.

For example:

{ "id": 6, "pid": 4 }

It generates this property first with id:

"6": {
    "id": 6,
    "pid": 4
}

and then with pid:

"4": {
    "children": [
        {
            "id": 6,
            "pid": 4
        }
    ]
},

and while all objects are similarly treated, we finally get a tree.

If id === pid, the root node is found. This is the object for the later return.

var data = [
        { "id": "f", "pid": "b", "name": "F" },
        { "id": "e", "pid": "c", "name": "E" },
        { "id": "d", "pid": "c", "name": "D" },
        { "id": "c", "pid": "b", "name": "C" },
        { "id": "a", "pid": "a", "name": "A" },
        { "id": "b", "pid": "a", "name": "B" }
    ],
    tree = function (data) {
        var r, o = Object.create(null);
        data.forEach(function (a) {
            a.children = o[a.id] && o[a.id].children;
            o[a.id] = a;
            if (a.id === a.pid) {
                r = a;
            } else {
                o[a.pid] = o[a.pid] || {};
                o[a.pid].children = o[a.pid].children || [];
                o[a.pid].children.push(a);
            }
        });
        return r;
    }(data);

console.log(tree);

这篇关于如何将平面多分支数据转换为分层 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆