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

150
Visualizações
Get the branch of collection without the sibling elements, searching by property

I have the object with the next structure:

let array = [
      {
        name: 'Name1',
        items: [
          {
            name: 'Name1.1',
            items: [
              { id: '1', name: 'Name1.1.1' },
              { id: '2', name: 'Name1.1.2' },
              { id: '3', name: 'Name1.1.3' },
              ...
            ],
          },
          {
            name: 'Name1.2',
            items: [
              { id: '4', name: 'Name1.2.1' },
              { id: '5', name: 'Name1.2.2' },
            ],
          },
        ],
      },
      {
        name: 'Name2',
        items: [
          {
            name: 'Name2.1',
            items: [
              { id: '6', name: 'Name2.1.1' },
              { id: '7', name: 'Name2.1.2' },
            ],
          },
        ],
      },
    ];

I want to get the branch without the sibling elements, searching by id. The desired result is the next structure by id = '4':

let array = [
      {
        name: 'Name1',
        items: [
          {
            name: 'Name1.2',
            items: [
              { id: '4', name: 'Name1.2.1' },
            ],
          },
        ],
      }
    ];

I could find only the end element of the tree ({ id: '4', name: 'Name1.2.1' }). But I don't understand how to get intermediate structures of the tree.

const test = (data, id) => {
    if (!data || !data.length) return null;

    for (var j = 0; j < data.length; j++) {
      var result = data[j].items
        ? test(data[j].items, id)
        : data[j].id
        ? data[j].id === id
          ? data[j]
          : undefined
        : undefined;

      if (result !== undefined) {
        return result;
      }
    }

    return undefined;
  };

test(array, '4');
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should indeed take a recursive approach, but your function currently can only return an id value (a string) or null or undefined. It never returns an array, yet that is what you expect to get.

When a solution is found as a base case, you need to wrap that solution in an array and plain object, each time you get out of the recursion tree.

Here is a working solution:

function getPath(forest, targetid) {
    for (let root of forest) {
        if (root.id === targetid) return [root]; // base case
        let items = root.items && getPath(root.items, targetid);
        if (items) return [{ ...root, items }];  // wrap!
    }
}

// Example run:
let array = [{name: 'Name1',items: [{name: 'Name1.1',items: [{ id: '1', name: 'Name1.1.1' },{ id: '2', name: 'Name1.1.2' },{ id: '3', name: 'Name1.1.3' },],},{name: 'Name1.2',items: [{ id: '4', name: 'Name1.2.1' },{ id: '5', name: 'Name1.2.2' },],},],},{name: 'Name2',items: [{name: 'Name2.1',items: [{ id: '6', name: 'Name2.1.1' },{ id: '7', name: 'Name2.1.2' },],},],},];
console.log(getPath(array, '4'));

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