How to find an object by a key (ej id) inside a multidimensional array?

1

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);
    
asked by Pablo Contreras 07.09.2017 в 08:08
source

1 answer

1

It is giving you an error because your function is returning undefined and, therefore, can not get an attribute (the array partes ) of something that does not exist.

I recommend that you perform a control (an if) to control that if graphic_array has content, show it on the screen and, if not, show nothing. Also, if graphic_array contains the array partes show it and, if not, do not show anything.

Since you are using a recursive function, you must also get the object that returns the function .

Subsequently, if the object has data, return it.

Your modified example:

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) {
  var objeto = {};
  
  for (n in array) {
    if(id === array[n].id){ 
      return array[n];
      break;
    }else if (array[n].partes) {
       objeto = obtener_array(array[n].partes, id);
    }
  }
  
  if(objeto){
     return objeto;
  }
}

let graphic_array = obtener_array(partes, 310);
if(graphic_array.id){
  console.log(graphic_array);
}else{
  console.log("El id no se encuentra en el array de objetos.");
}


if(graphic_array.partes){
  let graphic_datos = graphic_array.partes;
  console.log(graphic_datos);
}
    
answered by 07.09.2017 / 09:25
source