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

93
Vistas
Search in the multiply tree view doesn't work properly

have a problem with the implamantation search in the multiple tree view. Can check the code in the link. In the group we have childGroup and list, we have to search lists and groups names. (check data in the data.js file)

Think the problem is somewhere here.

const search = (items, term) => {
    return items.reduce((acc, item) => {
      if (contains(item.name, term)) {
        acc.push(item);
      } else if (item.childGroupList?.length) {
        let newGroupsItems = search(item.childGroupList, term);
        if (newGroupsItems?.length) {
          item.childGroupList = newGroupsItems;
          acc.push(item);
        }
      } else if (item.list?.length) {
        let newListItems = search(item.list, term);
        if (newListItems?.length) {
          item.list = newListItems;
          acc.push(item);
        }
      }

      return acc;
    }, []);
  };
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

There were a few issues in your recursive search.

  1. Your if, else if, else if chain should be independent. They should really be three if blocks.

  2. You need to push the result only after when all the searches are done at a certain level. I got a copy of the item and make childGroupList and list properties empty([]) initially and update them when a search is successful.

Try like this.

  // main search function, here is the problem
  const search = (items, term) => {
    return items.reduce((acc, item) => {
      let itemCopy = JSON.parse(JSON.stringify(item));
      itemCopy.childGroupList = [];
      itemCopy.list = [];
      let found = false;
      if (contains(item.name, term)) {
        found = true;
      }
      if (item.childGroupList?.length) {
        let newGroupsItems = search(item.childGroupList, term);
        if (newGroupsItems?.length) {
          found = true;
          itemCopy.childGroupList = newGroupsItems;
        }
      }
      if (item.list?.length) {
        let newListItems = search(item.list, term);
        if (newListItems?.length) {
          found = true;
          itemCopy.list = newListItems;
        }
      }
      if (found) {
        acc.push(itemCopy);
      }
      return acc;
    }, []);
  };

Code sandbox => https://stackblitz.com/edit/react-rmrlj1?file=src%2FApp.js

about 4 years ago · Santiago Gelvez 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