Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
Find a value in array of objects
  const objectTest = {
    Shoes: [
      { id: 1, name: "xxx", image: "xxxxxx" },
      { id: 2, name: "yyy", image: "yyyyyy" },
    ],
    Top: [
      { id: 1, name: "zzz", image: "zzzzzz" },
      { id: 2, name: "aaa", image: "aaaaaa" },
    ],
  };

I know i can use the find method.

 const item = objectTest.Shoes.find((p) => p.name === "xxx");
 console.log(item);  ///{id: 1, name: 'xxx', image: 'xxxxxx'}

but what if i don't know "xxx" belongs to the Shoes group. How do I find "xxx" and return { id: 1, name: "xxx", image: "xxxxxx" }

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Easiest way is to flatten the object values first:

Object.values(objectTest).flat()

then you can find it normally:

const item = Object.values(objectTest).flat().find((p) => p.name === "xxx");

If you want to know which "group" it belonged to, you must iterate over Object.entries(objectTest).

about 4 years ago · Juan Pablo Isaza Denunciar

0

If the schema's of Shoes and Top are the same, you can flatten them like

  const objectTest = {
    Shoes: [
      { id: 1, name: "xxx", image: "xxxxxx" },
      { id: 2, name: "yyy", image: "yyyyyy" },
    ],
    Top: [
      { id: 1, name: "zzz", image: "zzzzzz" },
      { id: 2, name: "aaa", image: "aaaaaa" },
    ],
  };

const findX = (objectTest, name) => {
   return [...objectTest.Shoes, ...objectShoes.Top].find((p) => p.name === "xxx");
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use for-in loop to traverse array, then can find into particular objects array.

const objectTest = {
    Shoes: [
      { id: 1, name: "xxx", image: "xxxxxx" },
      { id: 2, name: "yyy", image: "yyyyyy" },
    ],
    Top: [
      { id: 1, name: "zzz", image: "zzzzzz" },
      { id: 2, name: "aaa", image: "aaaaaa" },
    ],
  };
  
  let obj;
  for(let prop in objectTest) {
    obj = objectTest[prop].find(x=>x.name == "xxx");
    if(obj)
        break;    
  }
  
  console.log(obj)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda