Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda