作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如果我问了一个愚蠢的问题,我很抱歉,但这让我很困扰,我还无法找到最好的解决方案。
我有一个如下所示的 JSON 数据:
{
"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",
"type": "database",
"children": [
]
},
{
"name": "active_directory",
"type": "nfs",
"children": []
}
]
}
我正在尝试处理此数据,以便对于选定的根(名称),数据将存在层次结构关系。例如。结果数据应如下所示(如果我选择了“bugs_db”)。
{
"name": "bugs_db",
"kind": "root",
"children": [
{
"name": "oss",
"type": "ui",
"children": [
{
"name": "active-directory",
"type": "nfs",
"children": []
},
{
"name": "passive-directory",
"type": "FAT32"
}
]
},
{
"name" : "dashboard",
"type": "ui"
},
{
"name": "dev-dash",
"type": "ui"
}
]
}
我已经尝试编写可以工作到 1 级的代码..
var selectedApp = "bugs_db";
var all_data = {
name: selectedApp,
type: "root",
children: []
}
for(var i = 0; i < data.my_data.length; i++){
var currentObj = data.my_data[i];
if(currentObj.name == selectedApp && currentObj.children.length){
for(var j = 0; j < currentObj.children.length; j++){
let childObj = {
name: currentObj.children[j].name,
type: currentObj.children[j].type,
children: []
}
allData.children.push(childObj);
}
}
}
但是上面的代码并没有进一步的层次化。我知道这可以使用某种递归函数来完成......但我不确定如何......可能这就是为什么陷入无限循环的原因。
任何人都可以在这里帮助我。如果您需要更多信息,请告诉我。
最佳答案
您可以通过构建新对象来采用映射和递归方法。
function getTree(name) {
var object = map.get(name);
return object && Object.assign({}, object, { children: object.children.map(o => Object.assign({}, o, getTree(o.name))) });
}
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", type: "database", children: [] }, { name: "active_directory", type: "nfs", children: [] }] },
map = new Map(object.my_data.map(o => [o.name, o]));
console.log(getTree("bugs_db"));
.as-console-wrapper { max-height: 100% !important; top: 0; }
有循环检查。
function getTree(name, visited = new Set) {
var object = map.get(name);
if (!object) {
return object;
}
if (visited.has(name)) {
return object && Object.assign({}, object, { circular: true, children: [] });
}
visited.add(name);
return Object.assign({}, object, { children: object.children.map(o => Object.assign({}, o, getTree(o.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: "bugs_db", type: "exception" }] }, { name: "oss", type: "ui", children: [{ name: "active-directory", type: "nfs" }, { name: "passive-directory", type: "FAT32" }] }, { name: "jira_db", type: "database", children: [] }, { name: "active_directory", type: "nfs", children: [] }] },
map = new Map(object.my_data.map(o => [o.name, o]));
console.log(getTree("bugs_db"));
.as-console-wrapper { max-height: 100% !important; top: 0; }
关于javascript - 处理 JSON 以创建层次关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50490287/
我正在尝试将多个水平链接的 Button 和 TextView 垂直链接为 View 集,但仍保持平面 View 层次结构。这是我的初始布局和代码:
到目前为止,我已经在Google BigQuery上训练了几种模型,目前我需要查看模型的外观(即架构,损失函数等)。 有没有办法获取这些信息? 最佳答案 仔细阅读文档后,我可以说该功能尚不存在。我什至
本文实例讲述了PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)。分享给大家供大家参考,具体如下: 前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个
我是一名优秀的程序员,十分优秀!