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

184
Views
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
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!