【发布时间】:2022-01-04 18:21:02
【问题描述】:
我正在尝试迭代一个看起来像这样的 JSON 对象结构。
{
"id": 1,
"recursoId": {
"id": 1,
"tipoRecurso": {
"id": 5,
"nombre": "Base de datos PostgreSQL",
"icono": "fas fa-database"
},
"alias": "Base de datos Producción (Retama)",
"nombre": "Retama",
"propietario": {
"id": 4,
"nombre": "Sistemas"
},
"servicio012": false
},
"ip": "120",
"port": "1234",
"parent": null,
"children": [
{
"id": 2,
"recursoId": {
"id": 2,
"tipoRecurso": {
"id": 5,
"nombre": "Base de datos PostgreSQL",
"icono": "fas fa-database"
},
"alias": "Base de datos Preproducción (Faya)",
"nombre": "Faya",
"propietario": {
"id": 4,
"nombre": "Sistemas"
},
"servicio012": false
},
"ip": "101",
"port": "101",
"parent": null,
"children": [
{
"id": 4,
"recursoId": {
"id": 4,
"tipoRecurso": {
"id": 3,
"nombre": "Servidor aplicaciones Tomcat",
"icono": "fas fa-database"
},
"alias": "Servidor Producción (Tacoronte2)",
"nombre": "Tacoronte2",
"propietario": {
"id": 4,
"nombre": "Sistemas"
},
"servicio012": false
},
"ip": "103",
"port": "103",
"parent": null,
"children": [
{
"id": 8,
"recursoId": {
"id": 8,
"tipoRecurso": {
"id": 1,
"nombre": "Aplicación",
"icono": "fas fa-terminal"
},
"alias": "Inicio",
"nombre": "Inicio",
"propietario": {
"id": 1,
"nombre": "Formación"
},
"servicio012": true
},
"ip": "107",
"port": "107",
"parent": null,
"children": []
},
{
"id": 9,
"recursoId": {
"id": 8,
"tipoRecurso": {
"id": 1,
"nombre": "Aplicación",
"icono": "fas fa-terminal"
},
"alias": "Inicio",
"nombre": "Inicio",
"propietario": {
"id": 1,
"nombre": "Formación"
},
"servicio012": true
},
"ip": "108",
"port": "108",
"parent": null,
"children": []
}
]
},
{
"id": 5,
"recursoId": {
"id": 5,
"tipoRecurso": {
"id": 5,
"nombre": "Base de datos PostgreSQL",
"icono": "fas fa-database"
},
"alias": "Base de datos Preproducción (Faya)",
"nombre": "Tejina",
"propietario": {
"id": 4,
"nombre": "Sistemas"
},
"servicio012": true
},
"ip": "104",
"port": "104",
"parent": null,
"children": []
}
]
},
{
"id": 3,
"recursoId": {
"id": 3,
"tipoRecurso": {
"id": 5,
"nombre": "Base de datos PostgreSQL",
"icono": "fas fa-database"
},
"alias": "Base de datos Desarrollo (Tabaiba)",
"nombre": "Tabaiba",
"propietario": {
"id": 4,
"nombre": "Sistemas"
},
"servicio012": false
},
"ip": "102",
"port": "102",
"parent": null,
"children": [
{
"id": 6,
"recursoId": {
"id": 6,
"tipoRecurso": {
"id": 1,
"nombre": "Aplicación",
"icono": "fas fa-terminal"
},
"alias": "Buzón",
"nombre": "Buzon",
"propietario": {
"id": 1,
"nombre": "Formación"
},
"servicio012": false
},
"ip": "105",
"port": "105",
"parent": null,
"children": []
},
{
"id": 7,
"recursoId": {
"id": 7,
"tipoRecurso": {
"id": 1,
"nombre": "Aplicación",
"icono": "fas fa-terminal"
},
"alias": "Estadísticas",
"nombre": "Estadisticas",
"propietario": {
"id": 1,
"nombre": "Formación"
},
"servicio012": true
},
"ip": "106",
"port": "106",
"parent": null,
"children": []
}
]
}
]
}
所以这是一个树状的父子关系。每个节点都有一个唯一的 ID。我正在尝试使用递归函数遍历整个对象,但我对孩子们有一些问题。
这是我的代码,其中“instancias”是完整的对象
jsonAdapter(instancias: any) {
Object.entries(instancias).forEach((entry) => {
const [key, value] = entry;
if (key === 'recursoId') {
Object.entries(value).forEach((recursoIdEntry) => {
const [key, value] = recursoIdEntry;
if (key === 'tipoRecurso') {
Object.entries(value).forEach((tipoRecursoEntry) => {
const [key, value] = tipoRecursoEntry;
})
}
if (key === 'propietario') {
Object.entries(value).forEach((propietarioEntry) => {
const [key, value] = propietarioEntry;
})
}
})
}
if ((key === 'children') && value) {
for (let i = 0; i < entry.length; i++) {
this.jsonAdapter(value[i]);
}
}
});
}
我执行了我的代码,它正确地返回了树的第一个分支。但在那之后,该函数总是返回 undefined (也总是输入 if )。我不确定如何解决这个问题。谢谢!。
【问题讨论】:
-
你期待什么输出?
-
现在我只是尝试打印它以查看它是否工作,最后一个 if 是这样的。
console.log(`${key}: ${value}`);并查看所有 json。 -
将什么重命名为什么? (请清楚说明)。
-
我想操作 Keys,所以我想删除其中一些(servicio012)并重命名其他的,nombre-> label
标签: javascript json typescript recursion