gpt4 book ai didi

javascript - 在javascript中从平面数组构建树数组

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:08 32 4
gpt4 key购买 nike

我有一个复杂的 json 文件,我必须用 javascript 处理它以使其分层,以便以后构建树。json 的每个条目都有:id :一个唯一的ID,parentId :父节点的id(如果节点是树的根则为0)level : 树的深度级别

json 数据已经“有序”。我的意思是,一个条目将在其自身上方有一个父节点或兄弟节点,在其下方有一个子节点或兄弟节点。

输入:

{
"People": [
{
"id": "12",
"parentId": "0",
"text": "Man",
"level": "1",
"children": null
},
{
"id": "6",
"parentId": "12",
"text": "Boy",
"level": "2",
"children": null
},
{
"id": "7",
"parentId": "12",
"text": "Other",
"level": "2",
"children": null
},
{
"id": "9",
"parentId": "0",
"text": "Woman",
"level": "1",
"children": null
},
{
"id": "11",
"parentId": "9",
"text": "Girl",
"level": "2",
"children": null
}
],
"Animals": [
{
"id": "5",
"parentId": "0",
"text": "Dog",
"level": "1",
"children": null
},
{
"id": "8",
"parentId": "5",
"text": "Puppy",
"level": "2",
"children": null
},
{
"id": "10",
"parentId": "13",
"text": "Cat",
"level": "1",
"children": null
},
{
"id": "14",
"parentId": "13",
"text": "Kitten",
"level": "2",
"children": null
},
]
}

预期输出:

{
"People": [
{
"id": "12",
"parentId": "0",
"text": "Man",
"level": "1",
"children": [
{
"id": "6",
"parentId": "12",
"text": "Boy",
"level": "2",
"children": null
},
{
"id": "7",
"parentId": "12",
"text": "Other",
"level": "2",
"children": null
}
]
},
{
"id": "9",
"parentId": "0",
"text": "Woman",
"level": "1",
"children":
{

"id": "11",
"parentId": "9",
"text": "Girl",
"level": "2",
"children": null
}
}

],

"Animals": [
{
"id": "5",
"parentId": "0",
"text": "Dog",
"level": "1",
"children":
{
"id": "8",
"parentId": "5",
"text": "Puppy",
"level": "2",
"children": null
}
},
{
"id": "10",
"parentId": "13",
"text": "Cat",
"level": "1",
"children":
{
"id": "14",
"parentId": "13",
"text": "Kitten",
"level": "2",
"children": null
}
}

]
}

最佳答案

如果您使用 map 查找,则有一个有效的解决方案。如果 parent 总是在他们的 child 之前出现,您可以合并两个 for 循环。它支持多个根。它在悬挂分支上给出错误,但可以修改为忽略它们。它不需要第 3 方库。据我所知,这是最快的解决方案。

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;
}

var entries = [{
"id": "12",
"parentId": "0",
"text": "Man",
"level": "1",
"children": null
},
{
"id": "6",
"parentId": "12",
"text": "Boy",
"level": "2",
"children": null
},
{
"id": "7",
"parentId": "12",
"text": "Other",
"level": "2",
"children": null
},
{
"id": "9",
"parentId": "0",
"text": "Woman",
"level": "1",
"children": null
},
{
"id": "11",
"parentId": "9",
"text": "Girl",
"level": "2",
"children": null
}
];

console.log(list_to_tree(entries));

如果您对复杂性理论感兴趣,这个解是 Θ(n log(n))。递归滤波器解决方案是 Θ(n^2),这对于大型数据集来说可能是个问题。

关于javascript - 在javascript中从平面数组构建树数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56843959/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com