我有以下資料結構
[
{
"id": 3,
"name" "Important Topic 3",
"questions": [array of questions],
"topics: [array of topics],
"parentTopic": {
"id": 2,
"name": "Parent Topic 1",
"parentTopic": {
"id": 1,
"name": "Parent Topic 2",
"parentTopic: null
}
}
},
{
"id": 4,
"name" "Important Topic 4",
"questions": [array of questions],
"topics: [array of topics],
"parentTopic": {
"id": 2,
"name": "Parent Topic 1",
"parentTopic": {
"id": 1,
"name": "Parent Topic 2",
"parentTopic: null
}
}
}
]
我想結束以下
[
{
"id": 1,
"name": "Parent Topic 2",
"topics": [
{
"id": 2,
"name": "Parent Topic 1"
"topics": [
{
"id": 3,
"name: "Important Topic 3",
"questions": [array of questions],
"topics: [array of topics]
},
{
"id": 4,
"name: "Important Topic 4",
"questions": [array of questions],
"topics: [array of topics]
}
]
}
]
}
]
我有一些代碼,我也從很棒的 stackoverflow 社區獲得了幫助,但它只有在我有嵌套的字典物件時才有效,而不是陣列中的字典串列。這是stackoverflow參考 - Javascript中的反向嵌套樹
如果您能幫助解決這個難題,我將不勝感激:)
uj5u.com熱心網友回復:
您可以在地圖中收集節點(當您訪問它們時),以它們的 ID 為鍵,這樣您就可以知道何時遇到已放置在目標結構中的節點。因此,以深度優先順序(前序)訪問源節點并自下而上構建新的層次結構,但在遇到已經在目標結構中的節點時停止。此時將最后創建的目標節點放入其topics陣列中。
這是該想法的實作:
function invertHierarchy(leaves) {
const root = { id: null, topics: [] };
const map = { null: root };
for (let { id, parentTopic, ...rest } of leaves) {
let node = map[id] = { id, ...rest }; // leaf
while (true) {
({id, parentTopic, ...rest} = parentTopic ?? root);
if (id in map) break;
map[id] = node = { id, ...rest, topics: [node] };
}
map[id].topics.push(node);
}
return root.topics;
}
// Example run:
const leaves = [{"id": 3,"name": "Important Topic 3","questions": [],"topics": ["a"],"parentTopic": {"id": 2,"name": "Parent Topic 1","parentTopic": {"id": 1,"name": "Parent Topic 2","parentTopic": null}}},{"id": 4,"name": "Important Topic 4","questions": [],"topics": ["b"],"parentTopic": {"id": 2,"name": "Parent Topic 1","parentTopic": {"id": 1,"name": "Parent Topic 2","parentTopic": null}}}];
const result = invertHierarchy(leaves);
console.log(result);
uj5u.com熱心網友回復:
這似乎達到了你想要的:
// The json data
const jsonData = `[
{
"id": 3,
"name": "Important Topic 3",
"questions": [],
"topics": [],
"parentTopic": {
"id": 2,
"name": "Parent Topic 1",
"parentTopic": {
"id": 1,
"name": "Parent Topic 2",
"parentTopic": null
}
}
},
{
"id": 4,
"name": "Important Topic 4",
"questions": [],
"topics": [],
"parentTopic": {
"id": 2,
"name": "Parent Topic 1",
"parentTopic": {
"id": 1,
"name": "Parent Topic 2",
"parentTopic": null
}
}
}
]`
const baseData = JSON.parse(jsonData)
// List of all topics (children and parents)
let topics = []
// Recursive function to add children elements
const listTopics = topic => {
// Push only if it doesn't already exists to avoid duplicates
if (!topics.some(e => e.id === topic.id))
topics.push(topic)
// The recursive part
if (topic.parentTopic)
listTopics(topic.parentTopic)
}
// Ignite
baseData.forEach(listTopics)
// Runs while none of the topics elements has a parent
// You can replace it by `topics.length > 1` if you only have one root topic
while (topics.some(e => e.parentTopic)) {
const parent = topics.shift()
topics = topics.filter(topic => {
if (topic.parentTopic && topic.parentTopic.id === parent.id) {
if (!parent.topics)
parent.topics = []
parent.topics.push(topic)
return false
}
return true
})
topics.push(parent)
}
// Print the new tree as JSON with some modifications to avoid circulars between parents and children
console.log(JSON.stringify(topics, (key, value) => key === 'parentTopic' ? (value === null ? null : {
id: value.id
}) : value, ' '))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/531966.html
下一篇:如何在物件的嵌套陣列中獲取資料
