Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!