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

124
Visualizações
How to filter values of an array inside an array?

I will describe the situation for more clarity. I have an array like this

animalsArray = {
animalID : number,
animalName: string,
animalDescription: string,

animalChild: animalsArray[]

}

Now I have to filter these animals using animalName from user input by textfield. Some animals can have n number of animalChild or non at all or animalChild can have another animalChild inside it.

I already have a code like this

public animalsArray:animalsArray[]

    this.filteredanimalsArray.next(
                this.animalsArray.filter(item => (item.animalName.toLowerCase().indexOf(search) > -1))
            );

To filter the main animalsArray and it works fine but the user input doesn't go through the child arrays.

How can I solve this problem? Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Maybe you could use recursivity like this :

function filterAnimals(animals, name) {
    return animals.filter(animal => {
        const matching = animal.animalName.toLowerCase() === name.toLowerCase();
        const hasChildMatch = Array.isArray(animal.animalChild) && filterAnimals(animal.animalChild, name).length > 0;
        return matching || hasChildMatch;
    });
}

const search = 'leon';
const filterdAnimals = filterAnimals(animalsArray, search);
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