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

197
Views
Filter unknown deep nested array based on input value

First I must say sorry if this question is already answered, but I have not found the answer I am looking for :(

I have an array of objects with unknown nesting depth (it can be 20-30 or even more) and I want to filter it's 'name' property based on input field value.

public nestedArray = [
  {id: 1, name: 'Example_1', children: []},
  {id: 2, name: 'Test', children: []},
  {id: 3, name: 'Test Name', children: [
    {id: 10, name: 'Child name', children: [
      {id: 20, name: 'Example_14', children: []},
      {id: 30, name: 'Last Child', children: []}
        ]
      }
    ]
  }
];

The result I want to receive is an array of objects with only one level deep with 'name' field which includes input value. For example my input value is 'am', so the result would be:

resultsArray = [
  {id: 1, name: 'Example_1'},
  {id: 3, name: 'Test Name'},
  {id: 10, name: 'Child name'},
  {id: 20, name: 'Example_14'}
];

There is no problem to do it on the first level like that:

public filter(array: any[], input_value: string): void {
  array = array.filter(el => {
    return el.name.toLowerCase().includes(input_value.toLowerCase()));
  }
}

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could map the array and their children and take a flat result of objects where the string is matching the name property.

const
    find = value => ({ children, ...o }) => [
        ...(o.name.includes(value) ? [o] : []),
        ...children.flatMap(find(value))
    ],
    data = [{ id: 1, name: 'Example_1', children: [] }, { id: 2, name: 'Test', children: [] }, { id: 3, name: 'Test Name', children: [{ id: 10, name: 'Child name', children: [{ id: 20, name: 'Example_14', children: [] }, { id: 30, name: 'Last Child', children: [] }] }] }],
    result = data.flatMap(find('am'));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Another solution with a single result array and a classic approach.

const
    find = (array, value) => {
        const
            iter = array => {
                for (const { children, ...o } of array) {
                    if (o.name.includes(value)) result.push(o);
                    iter(children);
                }
            },
            result = [];

        iter(array);
        return result;
    },
    data = [{ id: 1, name: 'Example_1', children: [] }, { id: 2, name: 'Test', children: [] }, { id: 3, name: 'Test Name', children: [{ id: 10, name: 'Child name', children: [{ id: 20, name: 'Example_14', children: [] }, { id: 30, name: 'Last Child', children: [] }] }] }],
    result = find(data, 'am');

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!