处理JSON以创建子级与父级的层次关系 [英] Process JSON to create hierarchical relationship of child to parent
问题描述
我在这里得到了类似的答案,但我一直在寻找与之相反的东西,所以我从这个答案中寻求帮助来解释我的问题: 处理JSON以创建层次关系
I got the similar answer here but I was looking for something opposite of this, so I am taking help from this answer to explain my issue: Process JSON to create the hierarchical relationship
所以,我有一个类似这样的原始数据:
So, I have a raw data something like this:
{
"my_data": [
{
"name": "bugs_db",
"type": "database",
"children": [
{
"name": "oss",
"type": "ui"
},
{
"name": "dashboard",
"type": "ui"
},
{
"name": "dev-dash",
"type": "ui"
}
]
},
{
"name": "oss",
"type": "ui",
"children": [
{
"name": "active-directory",
"type": "nfs"
},
{
"name": "passive-directory",
"type": "FAT32"
},
{
"name": "jira_db",
"kind": "database"
}
]
},
{
"name": "jira_db",
"type": "database",
"children": [
{
"name": "oss",
"kind": "ui"
},
{
"name": "something",
"kind": "ui"
}
]
},
{
"name": "active_directory",
"type": "nfs",
"children": []
}
]
}
我需要处理以上数据以列出一个孩子的所有父母.例如,如果我选择名称" ="oss",则层次关系应该是这样的:
I need to process above data to list all the parents for a child.. for exa., if I select "name" = "oss", then the hierarchical relationship should be like this:
{
"name": "oss",
"type": "ui",
"parents": [
{
"name": "bugs_db",
"type": "database",
"parents": [
]
},
{
"name": "jira_db",
"type": "database"
}
]
}
也可以存在一些循环关系. bugs_db的oss子对象以及bugs_db的oss ..子对象.我想我可以尝试使用for循环进行处理并实现这一目标..但我正在寻找一些建议的类似解决方案: https://stackoverflow.com /a/50491255
Also there can be some circular relationship.. eg. oss child of bugs_db and bugs_db also child of oss.. in such case just skip further and add key "circular": true to the parent object. I think I can try processing using for loops and achieve this.. but I am looking for some similar solution suggested here: https://stackoverflow.com/a/50491255
推荐答案
基本上,您可以使用相同的方案来测试循环引用并过滤父级.
Basically you could use the same scheme which tests for circular reference and filters parents.
function getParents(name, visited = new Set) {
var item = map.get(name);
if (!item) {
return item;
}
if (visited.has(name)) {
return { name, type: item.type, circular: true };
}
visited.add(name);
return {
name,
type: item.type,
parent: object.my_data
.filter(({ children }) => children.some(o => o.name === name))
.map(({ name }) => getParents(name, visited))
};
}
var object = { my_data: [{ name: "bugs_db", type: "database", children: [{ name: "oss", type: "ui" }, { name: "dashboard", type: "ui" }, { name: "dev-dash", type: "ui" }] }, { name: "oss", type: "ui", children: [{ name: "active-directory", type: "nfs" }, { name: "passive-directory", type: "FAT32" }, { name: "jira_db", kind: "database" }] }, { name: "jira_db", type: "database", children: [{ name: "oss", kind: "ui" }, { name: "something", kind: "ui" }] }, { name: "active_directory", type: "nfs", children: [] }] },
map = new Map(object.my_data.map(o => [o.name, o]));
console.log(getParents("oss"));
.as-console-wrapper { max-height: 100% !important; top: 0; }
这篇关于处理JSON以创建子级与父级的层次关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!