бесконечной вложенностью? какая логика?
const comments = [
{
comment: 'Comment',
childs: [
{
comment: 'Child comment',
childs: [
]
}
]
}
]
С чего начать создание древовидных комментариев
var items = [
{
"Id": "1",
"Name": "q",
"Parent": "2"
},
{
"Id": "2",
"Name": "w",
"Parent": ""
},
{
"Id": "3",
"Name": "e",
"Parent": "5"
},
{
"Id": "4",
"Name": "r",
"Parent": "2"
},
{
"Id": "5",
"Name": "t",
"Parent": ""
},
{
"Id": "6",
"Name": "y",
"Parent": "2"
},
{
"Id": "7",
"Name": "u",
"Parent": "6"
},
{
"Id": "8",
"Name": "i",
"Parent": "6"
}
];
function buildHierarchy(arry) {
var roots = [], children = {};
// find the top level nodes and hash the children based on parent
for (var i = 0, len = arry.length; i < len; ++i) {
var item = arry[i],
p = item.Parent,
target = !p ? roots : (children[p] || (children[p] = []));
target.push({
value: item
});
}
// function to recursively build the tree
var findChildren = function (parent) {
if (children[parent.value.Id]) {
parent.children = children[parent.value.Id];
for (var i = 0, len = parent.children.length; i < len; ++i) {
findChildren(parent.children[i]);
}
}
};
// enumerate through to handle the case where there are multiple roots
for (var i = 0, len = roots.length; i < len; ++i) {
findChildren(roots[i]);
}
return roots;
}
console.log(buildHierarchy(items));
var data = {
name: 'My Tree',
children: [
{ name: 'hello' },
{ name: 'wat' },
{
name: 'child folder',
children: [
{
name: 'child folder',
children: [
{ name: 'hello' },
{ name: 'wat' }
]
},
{ name: 'hello' },
{ name: 'wat' },
{
name: 'child folder',
children: [
{ name: 'hello' },
{ name: 'wat' }
]
}
]
}
]
}
children: [
{ name: 'hello' },
{ name: 'wat' },
{
name: 'child folder',
children: [
{
name: 'child folder',
children: [
{ name: 'hello' },
{ name: 'wat' }
]
},
{ name: 'hello' },
{ name: 'wat' },
{
name: 'child folder',
children: [
{ name: 'hello' },
{ name: 'wat' }
]
}
]
}
]
а у меня функция buildHierarchy возвращает такой формат:На самом желе какой-то другой?children: [ { name: 'hello' }, { name: 'wat' }, ...
<details>
<summary>Коменты внутри</summary>
<p>
<details>
<summary>Первый</summary>
<p>Текст комментария 1</p>
</details>
<details>
<summary>Второй</summary>
<p>Текст комментария 2</p>
</details>
<details>
<summary>И внутри...</summary>
<p>
<details>
<summary>Третий</summary>
<p>Текст комментария 3</p>
</details>
</p>
</details>
</p>
</details>
var items = [
{
"Id": "1",
"Name": "q",
"Parent": "2"
},
{
"Id": "2",
"Name": "w",
"Parent": ""
},
{
"Id": "3",
"Name": "e",
"Parent": "5"
},
{
"Id": "4",
"Name": "r",
"Parent": "2"
},
{
"Id": "5",
"Name": "t",
"Parent": ""
},
{
"Id": "6",
"Name": "y",
"Parent": "2"
},
{
"Id": "7",
"Name": "u",
"Parent": "6"
},
{
"Id": "8",
"Name": "i",
"Parent": "6"
}
];
function buildHierarchy(arry) {
var roots = [], children = {};
// find the top level nodes and hash the children based on parent
for (var i = 0, len = arry.length; i < len; ++i) {
var item = arry[i],
p = item.Parent,
target = !p ? roots : (children[p] || (children[p] = []));
target.push({
value: item
});
}
// function to recursively build the tree
var findChildren = function (parent) {
if (children[parent.value.Id]) {
parent.children = children[parent.value.Id];
for (var i = 0, len = parent.children.length; i < len; ++i) {
findChildren(parent.children[i]);
}
}
};
// enumerate through to handle the case where there are multiple roots
for (var i = 0, len = roots.length; i < len; ++i) {
findChildren(roots[i]);
}
return roots;
}
console.log(buildHierarchy(items));
У вас что сообщения в файлике хранятся что-ли?
var items = [
{
"Id": "1",
"Name": "q",
"Parent": "2"
},
{
"Id": "2",
"Name": "w",
"Parent": ""
},
{
"Id": "3",
"Name": "e",
"Parent": "5"
},
{
"Id": "4",
"Name": "r",
"Parent": "2"
},
{
"Id": "5",
"Name": "t",
"Parent": ""
},
{
"Id": "6",
"Name": "y",
"Parent": "2"
},
{
"Id": "7",
"Name": "u",
"Parent": "6"
},
{
"Id": "8",
"Name": "i",
"Parent": "6"
}
];
И что еще за json сервер такой??
var data = {
name: 'My Tree',
children: buildHierarchy(items)
}
var data = { name: 'My Tree', children: buildHierarchy(items) }