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

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

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 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