gpt4 book ai didi

javascript - 从对象创建嵌套列表

转载 作者:行者123 更新时间:2023-11-29 09:52:04 24 4
gpt4 key购买 nike

我有以下对象

var json = {
items:[
{id:45 , lable:"Test 1.2.1", parent_id: 2},
{id:12, lable:"Test 1.0", parent_id: 0},
{id:32, lable:"Test 1.1", parent_id: 12},
{id:2, lable:"Test 1.2", parent_id: 12}
]
}

我需要使用这个对象来创建一个嵌套列表,它在浏览器中看起来像下面这样

  • 测试 1.0
    • 测试 1.1
    • 测试 1.2
      • 测试 1.2.1

我的方法是编写一个函数来遍历对象并将值存储在元素列表的数组中

function appendItems(json){
var list = json.items;
var listelem = [];

list.forEach(function(i){
var listelements = document.createElement('li');
listelements.setAttribute("id",i.id);
listelements.setAttribute("data-parent-id", i.parent_id);
listelements.innerText = i.lable;
listelem.push(listelements);

})

console.log(listelem);
}

但我不确定如何从这一点获得嵌套结构

最佳答案

我会首先将您的数组转换为一棵实际的树。你可以这样做:

var obj = {
items:[
{id:45 , lable:"Test 1.2.1", parent_id: 2},
{id:12, lable:"Test 1.0", parent_id: 0},
{id:32, lable:"Test 1.1", parent_id: 12},
{id:2, lable:"Test 1.2", parent_id: 12}
]
}

// first we create a dictionary so we can find elements by id easily
var objDict = obj.items.reduce(function(p,c) {
p[c.id] = c;
c.children = [];
return p;
}, {});

// then we build our tree
var tree = obj.items.reduce(function(p,c) {
// if the parent_id is zero, we have found the root.
if (!c.parent_id) {
p = c;
}
// otherwise, figure out who the parent is and add this one as a child
else {
objDict[c.parent_id].children.push(c);
}
return p;
}, {});

console.log(JSON.stringify(tree));

这会给你一个看起来像这样的对象:

{
"id": 12,
"lable": "Test 1.0",
"parent_id": 0,
"children": [
{
"id": 32,
"lable": "Test 1.1",
"parent_id": 12,
"children": []
},
{
"id": 2,
"lable": "Test 1.2",
"parent_id": 12,
"children": [
{
"id": 45,
"lable": "Test 1.2.1",
"parent_id": 2,
"children": []
}
]
}
]
}

现在应该很容易处理了。从根开始并创建一个节点,然后对每个子节点进行深度优先搜索,根据需要创建嵌套节点。

像这样:

processTree(tree, document.getElementById("list"));

function processTree(node, element) {
var li = document.createElement('li');
li.innerText = node.lable;
element.appendChild(li);
if (node.children.length) {
var ul = document.createElement('ul');
li.appendChild(ul);
// note: you might want / need to actual sort the children first
// but it's not clear from the OP if sorting by id will always give the right order
for (var i=0; i < node.children.length; i++) {
processTree(node.children[i], ul);
}
}
}

给你一个像这样的工作示例:

var obj = {
items: [{
id: 45,
lable: "Test 1.2.1",
parent_id: 2
}, {
id: 12,
lable: "Test 1.0",
parent_id: 0
}, {
id: 32,
lable: "Test 1.1",
parent_id: 12
}, {
id: 2,
lable: "Test 1.2",
parent_id: 12
}]
}

var objDict = obj.items.reduce(function(p, c) {
p[c.id] = c;
c.children = [];
return p;
}, {});

var tree = obj.items.reduce(function(p, c) {
if (!c.parent_id) {
p = c;
} else {
objDict[c.parent_id].children.push(c);
}
return p;
}, {});

processTree(tree, document.getElementById("list"));

function processTree(node, element) {
var li = document.createElement('li');
li.innerText = node.lable;
element.appendChild(li);
if (node.children.length) {
var ul = document.createElement('ul');
li.appendChild(ul);
for (var i = 0; i < node.children.length; i++) {
processTree(node.children[i], ul);
}
}
}
<ul id="list">
</ul>

关于javascript - 从对象创建嵌套列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33531301/

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