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

152
Vistas
How to know at what level a child is in deeply nested object?

I have recursive function which prints every child in a deeply nested object.

const tree = {
            name: "Anand",
            children: [
                {
                    name: "Dashrath",
                    children: [
                        {
                            name: "Sitesh",
                            children: [
                                {
                                    name: "Yadnesh",
                                    children: []
                                }
                            ]
                        }
                    ]
                },
                {
                    name: "Machindra",
                    children: [
                        {
                            name: "Tejas",
                            children: [
                                {
                                    name: "Tanishka",
                                    children: []
                                }
                            ],
                        },
                        {
                            name: "Amol",
                            children: [],
                        },
                        {
                            name: "Amit",
                            children: []
                        }
                    ]
                }
            ]
        }

        function printTree(t) {
            if (t.children.length === 0) {
                return
            }
            t.children.forEach((child,index) => {
                    console.log(child.name);                
                printTree(child);
            })
        }
        printTree(tree);
output : dashrath, sitesh, yadnesh, machindra, tanishka, amol, amit

and I want something like this 1st gen dashrath, 2nd gen sitesh, 3rd gen yadnesh, 1st gen machindra, 2nd gen tejas, 3rd gen tanishka, 2nd gen amol, 3rd gen amit

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could have the printTree function accept a depth parameter and increment it appropriately with each recursive call, something like this:

function printTree(t, depth = 0) {
    if (t.children.length === 0) {
        return
    }
    t.children.forEach((child,index) => {
        // you'd need to do some extra formatting here if you
        // want ordinals like "1st", "2nd", etc.
        console.log(`gen ${depth + 1}: ${child.name}`);                
        printTree(child, depth + 1);
    })
}
printTree(tree);
about 4 years ago · Juan Pablo Isaza Denunciar
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