当前位置:网站首页>JS从扁平array转tree
JS从扁平array转tree
2022-08-02 03:34:00 【IICOOM】
有时我们需要从扁平的数组结构(flat array),根据id,和pid构建出树形数组结构(tree array),这种情形经常出现在嵌套的目录结构,如下图:
或者企业的部门架构,也会出现上面的类似结构。
类似上面的情景,假如我们有以下的数据:
let entries = [{
"id": "12",
"parentId": "0",
"text": "Man",
"level": "1",
"children": null
},
{
"id": "7",
"parentId": "12",
"text": "Other",
"level": "2",
"children": null
},
{
"id": "8",
"parentId": "7",
"text": "Bird",
"level": "3",
"children": null
},
{
"id": "9",
"parentId": "0",
"text": "Woman",
"level": "1",
"children": null
},
{
"id": "11",
"parentId": "9",
"text": "Girl",
"level": "2",
"children": null
}
];
我们期待通过一个算法,构建出下面的结构:
[
{
"id": "12",
"parentId": "0",
"text": "Man",
"level": "1",
"children": [
{
"id": "7",
"parentId": "12",
"text": "Other",
"level": "2",
"children": [
{
"id": "8",
"parentId": "7",
"text": "Bird",
"level": "3",
"children": []
}
]
}
]
},
{
"id": "9",
"parentId": "0",
"text": "Woman",
"level": "1",
"children": [
{
"id": "11",
"parentId": "9",
"text": "Girl",
"level": "2",
"children": []
}
]
}
]
也许我们可以想到,在遍历数组的时候使用递归。没错可以解决问题,但是这种方式并不高效。
最近,在Stack Overflow上看到一个方法,感觉不错,贴出来分享一下:
function list_to_tree(list) {
var map = {
}, node, roots = [], i;
for (i = 0; i < list.length; i += 1) {
map[list[i].id] = i; // initialize the map
list[i].children = []; // initialize the children
}
for (i = 0; i < list.length; i += 1) {
node = list[i];
if (node.parentId !== "0") {
// if you have dangling branches check that map[node.parentId] exists
list[map[node.parentId]].children.push(node);
} else {
roots.push(node);
}
}
return roots;
}
算法的复杂度是Θ(n log(n))。
如果使用递归,那么复杂度是Θ(n^2)。如果数据集比较大,那么执行的时间会很长。
边栏推荐
猜你喜欢
随机推荐
【 LeetCode 】 design list
最第k大的数的一般性问题
2020 - AAAI - 图像修复 Image Inpainting论文导读 -《Region Normalization for Image Inpainting》
剑指Offer 32.Ⅲ从上到下打印二叉树
bluez5.50+pulseaudio实现蓝牙音响音频播放
【TCS3200 color sensor and Arduino realize color recognition】
Hash table problem solving method
unity学习(一):自动化创建模板脚本
引擎开发日志:场景编辑器开发难点
最长连续不重复子序列 双指针
TC358860XBG BGA65 东芝桥接芯片 HDMI转MIPI
实现动态库(DLL)之间内存统一管理
IDEA2021.2安装与配置(持续更新)
AD8307对数检波器
Pylon CLI 低成本的本地环境管控工具应用实例
剑指Offer 32.Ⅰ从上到下打印二叉树
剑指Offer 47.礼物的最大值 动态规划
剑指Offer 35.复杂链表的复制
【LeetCode】Add the linked list with carry
78XX 79XX多路输出电源