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

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

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