0

I have a json like:

[{
            "id": 11865,
            "parentid": null,
            "levelid": 63,
            "name": "Total"
        },
        {
            "id": 10143,
            "parentid": 11865,
            "levelid": 19,
            "name": "Productive"
        },
        {
            "id": 11873,
            "parentid": 10143,
            "levelid": 20,
            "name": "Regular"
        },
        {
            "id": -852,
            "parentid": 11873,
            "levelid": 255,
            "name": "801-REGULAR"
        },
        {
            "id": -888,
            "parentid": 11873,
            "levelid": 255,
            "name": "888-Regular 2"
        }]

And I need to change it some thing like that

[{
  "id":11865,
  "levelid":63,
  "parentid": null,
  "name": "Total",
  "children":[{ 
                "id":10143, 
                "parentid":11865, 
                "level":19, 
                "name": "Productive"
                "children":[{
                              "id": 10144,
                              "parentid":10143,
                              "levelid":20,
                              "name": "Other",
                              "children":[{......}]
             }];

This new json is based on parent child relation. Also I am implementing this on sever side in node js. Can Any one help on this ?

code Zero
  • 115
  • 2
  • 8

1 Answers1

4

A proposal which creates a tree and takes a given parentid as a root for the tree. This solution works for unsorted data.

How it works:

Basically for every object in the array it takes as well the id for building a new object as the parentid for a new object.

So for example

{ "id": 6, "parentid": 4 }

it generates first with id this property

"6": {
    "id": 6,
    "parentid": 4
}

and then this property with parentid

"4": {
    "children": [
        {
            "id": 6,
            "parentid": 4
        }
    ]
},

and while all object treated like this, we finally get a tree.

If parentid === root the root node is found. This is the object for the later return.

var data = [{ "id": 11865, "parentid": null, "levelid": 63, "name": "Total" }, { "id": 10143, "parentid": 11865, "levelid": 19, "name": "Productive" }, { "id": 11873, "parentid": 10143, "levelid": 20, "name": "Regular" }, { "id": -852, "parentid": 11873, "levelid": 255, "name": "801-REGULAR" }, { "id": -888, "parentid": 11873, "levelid": 255, "name": "888-Regular 2" }],
    tree = function (data, root) {
        var r, o = {};
        data.forEach(function (a) {
            a.children = o[a.id] && o[a.id].children;
            o[a.id] = a;
            if (a.parentid === root) {
                r = a;
            } else {
                o[a.parentid] = o[a.parentid] || {};
                o[a.parentid].children = o[a.parentid].children || [];
                o[a.parentid].children.push(a);
            }
        });
        return r;
    }(data, null);

document.write('<pre>' + JSON.stringify(tree, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392