I am trying to iterate a JSON object structure which looks like this.
{
"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": []
}
]
}
]
}
So it's a tree-like child-parent relation. Every node has a unique ID. I'm triying to iterate along the complete object with a recursive function but i have some problems with the children.
This is my code where "instancias" is the complete object
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]);
}
}
});
}
I execute my code and it returns the first branch of the tree correctly. But after that the function always returns undefined (Always enter to the if too). II'm not sure how to fix this. Thank you!.
CodePudding user response:
Well the problem you have is in the last if
statement, where you have a loop using entry.length
which will always return 2 since each entry has a key and value pair. Even if children is empty, entry equals ["children", []]
and its length is 2. You should use value.length instead as below:
if ((key === 'children') && value) {
for (let i = 0; i < value.length; i ) {
this.jsonAdapter(value[i]);
}
}
CodePudding user response:
This function "leaves" recursively iterates leaf key/values, passing the containing object for context. As an example here, the caller uses it to rename all of the "nombre" props to "renamed_nombre".
function leaves(obj, callback) {
for (let property in obj) {
if (obj.hasOwnProperty(property) && obj[property] != null) {
if (typeof obj[property] === 'object') {
leaves(obj[property], callback);
} else if (typeof obj[property] === 'array') {
for (let i = 0; i < obj[property].length; i ) {
leaves(obj[property][i], callback);
}
} else {
callback(obj, property, obj[property])
}
}
}
}
let bigObject = getBigObject()
leaves(bigObject, (object, key, value) => {
if (key === 'nombre') {
object.renamed_nombre = value
delete object[key]
}
})
console.log(JSON.stringify(bigObject, null, 4))
function getBigObject() {
return {
"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": []
}
]
}
]
}
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>