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

165
Vistas
Filter an array to return the deep level object

I have a deep level array like below,

const cat = [
  {
    name: "cat 1",
    id: 1,
    subCat: [
      {
        name: "sub 1",
        id: 10,
        subSubCat: [
          { summary: "summary1", id: 20 },
          { summary: "summary2", id: 21 },
        ],
      },
    ],
  },
  {
    name: "cat 2",
    id: 2,
    subCat: [
      {
        name: "sub 2",
        id: 20,
        subSubCat: [
          { summary: "summary1", id: 30 },
          { summary: "summary2", id: 31 },
        ],
      },
    ],
  },
];

I have to filter with subSubCat id(31) and it should return a summary value(summary2).

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

0

You can use Array.map and Array.flat to get the subSubCat list, then use Array.find to find the subSubCat item by id.

const cat = [
  {
    name: "cat 1",
    id: 1,
    subCat: [
      {
        name: "sub 1",
        id: 10,
        subSubCat: [
          { summary: "summary1", id: 20 },
          { summary: "summary2", id: 21 },
        ],
      },
    ],
  },
  {
    name: "cat 2",
    id: 2,
    subCat: [
      {
        name: "sub 2",
        id: 20,
        subSubCat: [
          { summary: "summary1", id: 30 },
          { summary: "summary2", id: 31 },
        ],
      },
    ],
  },
];

var subSubCats = cat.map(item => item.subCat.map(subCatItem => subCatItem.subSubCat)).flat(2);
var result = subSubCats.find(e => e.id == 31);
console.log(result.summary);

about 4 years ago · Juan Pablo Isaza Denunciar

0

This looks like something that requires a two level loop and find().

const cat = [
  {
    name: "cat 1",
    id: 1,
    subCat: [
      {
        name: "sub 1",
        id: 10,
        subSubCat: [
          { summary: "summary1", id: 20 },
          { summary: "summary2", id: 21 },
        ],
      },
    ],
  },
  {
    name: "cat 2",
    id: 2,
    subCat: [
      {
        name: "sub 2",
        id: 20,
        subSubCat: [
          { summary: "summary1", id: 30 },
          { summary: "summary2", id: 31 },
        ],
      },
    ],
  },
];


for(let i =0 ; i < cat.length;i++){
  if(Array.isArray(cat[i].subCat)){
  let subCatArray = cat[i].subCat;
  //console.log(subCatArray);
for(let j =0 ; j < subCatArray.length;j++){
  let subSubArray = subCatArray[j].subSubCat;
  if(Array.isArray(subSubArray)){
  //console.log(subSubArray);
  let foundObject = subSubArray.find((x) => 
   x.id === 31
  );
  //console.log(foundObject);
  if(foundObject!==undefined) 
   {
    console.log(foundObject.summary);
    break;
   }
   }
  }
}
}

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