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

162
Visualizações
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 Respostas
Responde à pergunta

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 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