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

180
Visualizações
Flat object to a nested object using JavaScript

Edit: to be clear, my input data is a JSON object with key-value pairs unlike the other similar posts here and this is the main reason I'm asking this question separately.

I haven't been able to figure out how to turn a flat object into a nested object using either BF, BFS, or DFS. There are multiple examples including arrays, but my case is a bit different due to the structure not being exactly list-like. I'm only doing this transformation due to a third-party library's requirements

Any help would be appreciated!

Input format:

{
    "id_1": {
        "title": "Title 1",
        "parent": null,
    },
    "id_2": {
        "title": "Title 2",
        "parent": "id_1",
    },
    "id_3": {
        "title": "Title 3",
        "parent": "id_2",
    }
}

Desired output format:

{
    "id": "id_1",
    "title": 'Title 1',
    children: [
    {
        "id": "id_2",
        "title": "Title 2",
        children: [
        {
            "id": "id_3",
            title: "Title 3",
            children: []
        }]
    }]
}

about 4 years ago · Juan Pablo Isaza
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