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

142
Views
I have an object that contains the following data. How can I get all the data in the title key using the recursive function?

I have an object that contains the following data. How can I get all the data in the title key using the recursive function? I want a function return a array include a, b, c, d, e, f

{
                children: [
                    {
                        children: [
                            {
                                children: [],
                                title: "a"
                            }
                        ],
                        title: "b"
                    },
                    {
                        children: [
                            {
                                children: [],
                                title: "c"
                            },
                            {
                                children: [],
                                title: "d"
                            }
                        ],
                        title: "e"
                    }
                ],
                title: "f"
            }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can do something like this:

function drill(t, n) { 
    if (n.length > 0) { 
        for (let elem of n) { 
            t.push(elem.title); 
            drill(t, elem.children) 
        } 
    } 
    return t 
}

As @rickhg12hs did here

about 4 years ago · Juan Pablo Isaza Report

0

Following a solution using modern syntax. extractTitles recursively produces arrays of titles as long as there are children and reduces them to a single array.

const extractTitles = (arr) =>
  arr.reduce((prev, { children, title }) => [...prev, title, ...(children.length ? extractTitles(children) : [])], []);

extractTitles(tree);

Or you can use flatmap.

const extractTitles = ({ title, children = [] }) =>
  [title, ...children.flatMap(extractTitles)];

extractTitles(tree);
// ["f","b","a","e","c","d"]
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!