Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda