Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda