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

159
Visualizações
Create a flat array representing the path to a certain index in a nested object array

I need to create a breadcrumb config list, depending on which item the user has clicked on in a list.

The items are listed as a sort of expandable table, where the user can click the name, then it expands with its children, then if the user clicks the deeper item, the same thing happens, etc.

Then if, for example, the user clicks on the sub level 3-1 breadcrumb, it should look like this:

Root level > sub level 1-1 > sub level 2-1 > sub level 3-1

or if the user clicks sub level 1-3, it should be:

Root level > sub level 1-3

What I got is the Id of the clicked item and the root level object.

I need to find in which place the item is, then loop up from its index to "Root level", collecting Name and Id on the way, creating a breadCrumbArry; something like this:

[
 {
  id:"1-1"
  name: "sub level 1-1"
 },
 {
  id:"2-1"
  name: "sub level 2-1"
 },
 {
  id:"3-1"
  name: "sub level 3-1"
 }
]

I've tried many different things, like flatten it and looping through it, collecting data on the way, but can't come up with a working solution. Now my brain has stopped functioning.. :) and a little help would be much appreciated.

The nested array looks like this and can have any depth:

 {
    Id: 1,
    Name:"Root level",
    Children: [
      {
        Id: 1,
        Name:"sub level 1-1",
        Children: [    
          {
           Id: 1,
           Name:"sub level 2-1",
           Children: [
               {
                Id: 1,
                Name:"sub level 3-1",
                Children: []
               },
             ]
         },
        ]
      },
      {
        Id: 2,
        Name:"sub level 1-2",
        Children: []
      },
      {
        Id: 3,
        Name:"sub level 1-3",
        Children: []
      },
    ]
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I didn't quite get how the id properties are derived in the output, but it looks like the concatenation of depth (level in the tree) and the node's own id value.

You would perform a depth first traversal through the whole menu. I assume there is in general no clue in the name of the target element that tells you anything about its location in the menu. So a full traversal may be needed. Then when you have a hit, start tracking back out of recursion, building the breadcrumb during this process.

function pathTo(menu, name, depth=1) {
    if (menu.Name === name) return [];
    for (let submenu of menu.Children) {
        let result = pathTo(submenu, name, depth+1);
        if (result) {
            return [{ 
                id: `${depth}-${submenu.Id}`,
                name: submenu.Name 
            }, 
            ...result];
        }
    }
}

let menu = {Id: 1,Name:"Root level",Children: [{Id: 1,Name:"sub level 1-1",Children: [{Id: 1,Name:"sub level 2-1",Children: [{Id: 1,Name:"sub level 3-1",Children: []},]},]},{Id: 2,Name:"sub level 1-2",Children: []},{Id: 3,Name:"sub level 1-3",Children: []},]};

let result = pathTo(menu, "sub level 3-1");
console.log(result);

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