Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!