如何将平面多分支数据转换为分层 JSON?

编程入门 行业动态 更新时间:2024-10-15 00:24:21
本文介绍了如何将平面多分支数据转换为分层 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 和一个pid,pid 指向这个人的更高级别的人的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?

本文发布于:2023-11-30 03:38:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   如何将   分支   平面   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!