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

122
Visualizações
Display nested html table looping through the nested array of objects

As the input, I have an array of objects. I need to loop through it and display the table as shown below:

const arr = [
  {
    name: 'home/',
    children: [
      {
        name: 'test1',
        children: [
          {
            name: 'test3',
            children: []
          }
        ]
      },
      {
        name: 'test2',
        children: []
      }
    ]
  }
]

|      | test1 | test3 |
| home |       |       |
|      | test2 |       |

 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You may try by nested tables. You can also try borders on tr or td elements when border-collapse: collapse is set on the table element CSS.

var arr = [ { name: 'home/'
            , children: [ { name: 'test1'
                          , children: [ { name: 'test3'
                                        , children: []
                                        }
                                      ]
                          }
                          ,
                          { name: 'test2'
                          , children: []
                          }
                        ]
            }
          ];
function makeTable(rows){
  return new DocumentFragment().appendChild(rows.reduce(function(t,r){
                                                          var tr = document.createElement("tr"),
                                                              td = document.createElement("td");
                                                          t.appendChild(tr)
                                                           .appendChild(td)
                                                           .append(r.name);
                                                          r.children.length && tr.appendChild(makeTable(r.children))
                                                          return t;
                                                        }, document.createElement("table")))
}
document.getElementById("container")
        .appendChild(makeTable(arr));
table {
  border: 1px solid black;
}
<div id="container"></div>

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