我有以下資料結構
[
{
id: 1,
name: 'Top Level Topic 1',
parentTopic: undefined
},
{
id: 2,
name: 'Some topic internally',
parentTopic: 1
},
{
id: 3,
name: 'Another topic',
parentTopic: 2
},
{ id: 4, name: 'Just another topic', parentTopic: 2 },
{
id: 5,
name: 'Another topic',
parentTopic: 1
},
{
id: 6,
name: 'Another topic',
parentTopic: 5
},
{
id: 7,
name: 'Another topic',
parentTopic: 5
},
{ id: 8, name: 'Another topic', parentTopic: 1 },
{
id: 9,
name: 'Another topic',
parentTopic: 8
},
{
id: 10,
name: 'Another topic',
parentTopic: 9
},
{
id: 11,
name: 'Another topic',
parentTopic: 10
},
{
id: 12,
name: 'Another Top Level Topic',
parentTopic: undefined
},
{ id: 13, name: 'Another Important Topic', parentTopic: 12 }]
我正在嘗試以下列方式轉換和構建它
[
{
id: 1,
name: 'Top Level Topic 1',
parentTopic: undefined,
index: 1,
children: [
{
id: 2,
name: 'Some topic internally',
parentTopic: 1,
index: 1.1,
children: [
{
id: 3,
name: 'Another topic',
parentTopic: 2,
index: 1.1.1,
children: []
},
{
id: 4,
name: 'Just another topic',
parentTopic: 2,
index: 1.1.2,
children: []
},
]
},
{
id: 5,
name: 'Another topic',
parentTopic: 1,
index: 1.2,
children: [
{
id: 6,
name: 'Another topic',
parentTopic: 5,
index: 1.2.1,
children: []
},
{
id: 7,
name: 'Another topic',
parentTopic: 5,
index: 1.2.2,
children: []
},
]
},
{
id: 8,
name: 'Another topic',
parentTopic: 1,
index: 1.3,
children: [
{
id: 9,
name: 'Another topic',
parentTopic: 8,
index: 1.3.1,
children: [
{
id: 10,
name: 'Another topic',
parentTopic: 9,
index: 1.3.1.1,
children: []
},
]
},
]
},
]
},
{
id: 12,
name: 'Another Top Level Topic',
parentTopic: undefined,
index: 2
children: [
{
id: 13,
name: 'Another Important Topic',
parentTopic: 12,
index: 2.1,
children: []
},
]
},
]
我的挑戰是我不確定如何遞回執行此操作。同樣在輸出中,您會注意到一個索引,它可以很好地在迭代時生成,或者它可能只是來自資料庫,這意味著我的原始資料結構已經有了它。
如果有人可以幫助我,我將不勝感激:)
這是我的代碼,但在頂層它是一個字典而不是字典串列
const invertHierarchy = (arr) => {
const map = {};
let root;
for (const ele of arr) {
map[ele.id] = ele;
ele.topics = [];
}
for (const ele of arr) {
if (map[ele.parentTopic] != null) map[ele.parentTopic].topics.push(ele);
else root = ele;
}
return root;
};
uj5u.com熱心網友回復:
const data = [{"id":1,"name":"Top Level Topic 1"},{"id":2,"name":"Some topic internally","parentTopic":1},{"id":3,"name":"Another topic","parentTopic":2},{"id":4,"name":"Just another topic","parentTopic":2},{"id":5,"name":"Another topic","parentTopic":1},{"id":6,"name":"Another topic","parentTopic":5},{"id":7,"name":"Another topic","parentTopic":5},{"id":8,"name":"Another topic","parentTopic":1},{"id":9,"name":"Another topic","parentTopic":8},{"id":10,"name":"Another topic","parentTopic":9},{"id":11,"name":"Another topic","parentTopic":10},{"id":12,"name":"Another Top Level Topic"},{"id":13,"name":"Another Important Topic","parentTopic":12}];
const getPrefix = (prefix, i) => prefix ? `${prefix}.${i 1}` : `${i 1}`
const f = (arr, parentTopic, prefix) =>
arr.filter(e=>e.parentTopic===parentTopic).map((e,i)=>({
...e,
index: getPrefix(prefix,i),
children: f(arr, e.id, getPrefix(prefix,i))
}))
console.log(f(data))
uj5u.com熱心網友回復:
您可以采用另一種方法,將所有節點及其父節點存盤在一個物件中,并只采用沒有父節點的陣列作為結果。
通過使用單個回圈,它還可以動態構建索引。
const
data = [{ id: 1, name: 'Top Level Topic 1', parentTopic: undefined }, { id: 2, name: 'Some topic internally', parentTopic: 1 }, { id: 3, name: 'Another topic', parentTopic: 2 }, { id: 4, name: 'Just another topic', parentTopic: 2 }, { id: 5, name: 'Another topic', parentTopic: 1 }, { id: 6, name: 'Another topic', parentTopic: 5 }, { id: 7, name: 'Another topic', parentTopic: 5 }, { id: 8, name: 'Another topic', parentTopic: 1 }, { id: 9, name: 'Another topic', parentTopic: 8 }, { id: 10, name: 'Another topic', parentTopic: 9 }, { id: 11, name: 'Another topic', parentTopic: 10 }, { id: 12, name: 'Another Top Level Topic', parentTopic: undefined }, { id: 13, name: 'Another Important Topic', parentTopic: 12 }],
tree = function(data, root) {
const t = {};
data.forEach(o => {
Object.assign(t[o.id] ??= {}, { ...o });
((t[o.parentTopic] ??= {}).children ??= []).push(t[o.id]);
const index = t[o.parentTopic].index || '';
t[o.id].index = index (index && '.') t[o.parentTopic].children.length;
});
return t[root].children;
}(data, undefined);
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/537099.html
上一篇:為什么生成器運算式與python3中的映射物件相比具有較低的耐力,同時重復出現,盡管有更多的pythonic?
下一篇:如何搜索基于物件的樹?
