Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:23:33+00:00 2026-06-10T07:23:33+00:00

i have a JSON array have the following data structure Jobs: [ { id:

  • 0

i have a JSON array have the following data structure

"Jobs":
        [
            { "id": "1", "JobTitle": "Engineer", "PID": "null" },
            { "id": "2", "JobTitle": "Project Manager", "PID": "null" },
            { "id": "5", "JobTitle": "Auditing Manager", "PID": "2" },
            { "id": "7", "JobTitle": "Auditor", "PID": "5" },
            { "id": "6", "JobTitle": "QA Manager", "PID": "5" },
            { "id": "3", "JobTitle": "QA", "PID": "6" },
            { "id": "4", "JobTitle": "Business Analyst", "PID": "2" }
        ]

i want to write a java script using Jquery and Knockoutjs (optional) to build a team structure (organization) with javascript and html, i have like 1000 record i have tried many recursive loops to handle it with no success.

the out put should be like this

<ul id="root">
<li>Engineer</li> //since their pid is null, then they are root nodes ( yeah not only root)
<li>Project Manager</li>
   <ul>
   <li>Auditing Manager</li>
   <li>Business Analyst</li>
   </ul>

and so on, it should handle many levels (depth), somebody will suggest DFS or BFS but i couldn’t implement them successfully.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:23:34+00:00Added an answer on June 10, 2026 at 7:23 am

    It’s midnight, I’m tired, but I can not refuse this challenge. It may not be the fastest solution, but the result is good (http://jsfiddle.net/NbRzB/1/) :

    function printNode(jobs, tree, id)
    {
        var html = '<li>' + jobs[id]['JobTitle'] + '</li>';
    
        if(tree[id] instanceof Array)
        {
            html += '<li><ul>';
    
            for(var i=0; i<tree[id].length; i++)
            {
                html += printNode(jobs, tree, tree[id][i]);
            }
    
            html += '</ul></li>';
        }
    
        return html;
    }
    
    var jobs =
    [
        { 'id': '1', 'JobTitle': 'Engineer', 'PID': 'null' },
        { 'id': '2', 'JobTitle': 'Project Manager', 'PID': 'null' },
        { 'id': '5', 'JobTitle': 'Auditing Manager', 'PID': '2' },
        { 'id': '7', 'JobTitle': 'Auditor', 'PID': '5' },
        { 'id': '6', 'JobTitle': 'QA Manager', 'PID': '5' },
        { 'id': '3', 'JobTitle': 'QA', 'PID': '6' },
        { 'id': '4', 'JobTitle': 'Business Analyst', 'PID': '2' }
    ];
    
    // tmp is used to build a better structure id => { attributes }
    var tmp = {};
    
    for(var i=0; i<jobs.length; i++)
    {
        tmp[jobs[i]['id']] =
        {
            'JobTitle' : jobs[i]['JobTitle'],
            'PID' : jobs[i]['PID']
        }
    }
    
    jobs = tmp;
    // end - build better structure
    
    // id => { child_id, child_id, ...}
    var tree = {};
    // { root_id, root_id, ...}
    var root = [];
    
    for(var id in tmp)
    {
        // no pid ? it is a root
        if(jobs[id]['PID'] == 'null')
        {
            root.push(id);
        }
        else
        {
            // Add "id" to "jobs[id]['PID']"'s children
            if(tree[jobs[id]['PID']] instanceof Array)
            {
                tree[jobs[id]['PID']].push(id);
            }
            else
            {
                tree[jobs[id]['PID']] = [ id ];
            }
        }
    }
    
    // recursive way
    var html = '<ul id="root">';
    
    for(var i=0; i<root.length; i++)
    {
        html += printNode(jobs, tree, root[i]);
    }
    
    html += '</ul>';
    
    // output
    $('body').append(html);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following Code package cyclist.project; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException;
I have something like a json array output, it comes like : { data:
I have a a following data that I want to convert it to JSON
I have the following data: [{class:test,description:o hai,example:a,banana:b}] As this JSON data is already in
I have the following json data in a Javascript variable result : [ Object
I'm new to the GSON library, I have a json data structure that looks
I have a class structure in C#, similar to the following: [DataContract] class Data
I have the following structure, and as you can see the methods data is
All, I have the following JSON Data. I need help writing a function in
I have this following method that connectss to db and returns a json array.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.