I am using a recursive function which consists of looking for a array
for its key id
, when it gets cut the repetition of it and returns the array
. It's the only way I can think of to find the array
that is what I need. If there is another way, welcome it, but I did not get anything and that's what I came up with.
The problem I have is that it returns undefined .
let partes = [
{
"id": 317,
"nombre": "Estación de Corte",
"padre": 0,
"conteo": 2,
"partes": [
{
"id": 320,
"nombre": "Ventosas",
"padre": 317,
"conteo": 3
}
]
},
{
"id": 1,
"nombre": "Estación de Formación",
"padre": 0,
"conteo": 2,
"partes": [
{
"id": 8,
"nombre": "Placas",
"padre": 1,
"conteo": 1,
"partes": [
{
"id": 305,
"nombre": "Superior ",
"padre": 8,
"conteo": 2
},
{
"id": 306,
"nombre": "Inferior",
"padre": 8,
"conteo": 2
}
]
}
]
},
{
"id": 4,
"nombre": "Estación de Calentamiento",
"padre": 0,
"conteo": 1,
"partes": [
{
"id": 7,
"nombre": "Placas",
"padre": 4,
"conteo": 1,
"partes": [
{
"id": 9,
"nombre": "Placa Superior de Calentamiento",
"padre": 7,
"conteo": 1
},
{
"id": 10,
"nombre": "Placa Inferior de Calentamiento",
"padre": 7,
"conteo": 1
}
]
}
]
},
{
"id": 341,
"nombre": "Sistema de Descarte",
"padre": 0,
"conteo": 2,
"partes": [
{
"id": 342,
"nombre": "Dedos de Retension",
"padre": 341,
"conteo": 2
}
]
},
{
"id": 334,
"nombre": "Sistema de Refrigeracion",
"padre": 0,
"conteo": 2,
"partes": [
{
"id": 336,
"nombre": "Evaporador",
"padre": 334,
"conteo": 1
},
{
"id": 335,
"nombre": "Compresor",
"padre": 334,
"conteo": 1
},
{
"id": 338,
"nombre": "Condensadora",
"padre": 334,
"conteo": 1
},
{
"id": 337,
"nombre": "Partes Electricas",
"padre": 334,
"conteo": 2
}
]
},
{
"id": 339,
"nombre": "Sistema de Deteccion",
"padre": 0,
"conteo": 10,
"partes": [
{
"id": 340,
"nombre": "Camaras",
"padre": 339,
"conteo": 10
}
]
},
{
"id": 309,
"nombre": "Estación de Sellado",
"padre": 0,
"conteo": 2,
"partes": [
{
"id": 310,
"nombre": "Placas",
"padre": 309,
"conteo": 5,
"partes": [
{
"id": 311,
"nombre": "Inferior",
"padre": 310,
"conteo": 5
},
{
"id": 312,
"nombre": "Superior",
"padre": 310,
"conteo": 5
}
]
},
{
"id": 315,
"nombre": "Leva",
"padre": 309,
"conteo": 1
},
{
"id": 314,
"nombre": "Muelles",
"padre": 309,
"conteo": 2
},
{
"id": 313,
"nombre": "Esparragos",
"padre": 309,
"conteo": 1
},
{
"id": 316,
"nombre": "Resortes",
"padre": 309,
"conteo": 1
}
]
}
];
function obtener_array(array, id) {
for (n in array) {
if(id === array[n].id){
return array[n];
break;
}
if (array[n].partes) {
obtener_array(array[n].partes, id);
}
}
}
let graphic_array = (obtener_array(partes, 310));
console.log(graphic_array);
let graphic_datos = graphic_array.partes;
console.log(graphic_datos);