Node.js - Convert flat json to hierarchical json without 'parent','child' attributes

前提是你 提交于 2020-04-14 18:11:05

问题


I want to build a hierarchical data structure from my sql query. After following this post for build hierarchical JSON object from flat objects, I try to create 4 level hierarchy with more attributes in object levels, but don't need children property.

How can i do this?

Here is my JavaScript (NodeJS) code:

var levels = ["counties_id","district_id", "municipalities_id", "institutes_id"];
data.forEach(function(d){
    var depthCursor = newData.counties;
    levels.forEach(function( property, depth ){
        var index;
        depthCursor.forEach(function(child,i){
            if ( d[property] == child.counties_id ) index = i;
        });
        if ( isNaN(index) ) {
            var propname = levels[depth];
            var obj = {};
            obj[propname] = d[property];
            obj["children"] = [];
            depthCursor.push(obj);
            index = depthCursor.length - 1;
        }
        depthCursor = depthCursor[index].children;
        if ( depth === levels.length - 1 ) {
            depthCursor.push({ id : d.id, name : d.name, name_H : d.name_h });
        }
    });
});

First level is ok because this equalation check the same first level attributes:

if ( d[property] == child.counties_id ) index = i;

How can i chect 2th, 3th and 4th level same case?

This is the flat object which keep trying:

[
{
"counties_h":"Megye 1",
"counties_en":"Coun 1",
"counties_id":"1",
"district_h":"Korz 1",
"district_en":"Dist 1",
"district_id":"101",
"municipalities_h":"Onk 1",
"municipalities_en":"Mun 1",
"municipalities_id":"asdf",
"institutes_h":"Int 1",
"institites_en":"Inst 1",
"institutes_id":"1"
},
{
"counties_h":"Megye 1",
"counties_en":"Coun 1",
"counties_id":"1",
"district_h":"Korz 1",
"district_en":"Dist 1",
"district_id":"101",
"municipalities_h":"Onk 1",
"municipalities_en":"Mun 1",
"municipalities_id":"asdf",
"institutes_h":"Int 2",
"institites_en":"Inst 2",
"institutes_id":"2"
},
{
"counties_h":"Megye 1",
"counties_en":"Coun 1",
"counties_id":"1",
"district_h":"Korz 1",
"district_en":"Dist 1",
"district_id":"101",
"municipalities_h":"Onk 2",
"municipalities_en":"Mun 2",
"municipalities_id":"sdfg",
"institutes_h":"Int 1",
"institites_en":"Inst 1",
"institutes_id":"1"
},
{
"counties_h":"Megye 1",
"counties_en":"Coun 1",
"counties_id":"1",
"district_h":"Korz 1",
"district_en":"Dist 1",
"district_id":"101",
"municipalities_h":"Onk 2",
"municipalities_en":"Mun 2",
"municipalities_id":"sdfg",
"institutes_h":"Int 2",
"institites_en":"Inst 2",
"institutes_id":"2"
},
{
"counties_h":"Megye 1",
"counties_en":"Coun 1",
"counties_id":"1",
"district_h":"Korz 2",
"district_en":"Dist 2",
"district_id":"102",
"municipalities_h":"Onk 1",
"municipalities_en":"Mun 1",
"municipalities_id":"dfgh",
"institutes_h":"Int 1",
"institites_en":"Inst 1",
"institutes_id":"1"
},
{
"counties_h":"Megye 1",
"counties_en":"Coun 1",
"counties_id":"1",
"district_h":"Korz 2",
"district_en":"Dist 2",
"district_id":"102",
"municipalities_h":"Onk 1",
"municipalities_en":"Mun 1",
"municipalities_id":"dfgh",
"institutes_h":"Int 2",
"institites_en":"Inst 2",
"institutes_id":"2"
},
{
"counties_h":"Megye 2",
"counties_en":"Coun 2",
"counties_id":"2",
"district_h":"Korz 2",
"district_en":"Dist 2",
"district_id":"202",
"municipalities_h":"Onk 1",
"municipalities_en":"Mun 1",
"municipalities_id":"fghj",
"institutes_h":"Int 1",
"institites_en":"Inst 1",
"institutes_id":"1"
},
{
"counties_h":"Megye 2",
"counties_en":"Coun 2",
"counties_id":"2",
"district_h":"Korz 2",
"district_en":"Dist 2",
"district_id":"202",
"municipalities_h":"Onk 1",
"municipalities_en":"Mun 1",
"municipalities_id":"fghj",
"institutes_h":"Int 2",
"institites_en":"Inst 2",
"institutes_id":"2"
}
]

Output of my current code:

{
   "counties":[
      {
         "counties_id":"1",
         "children":[
            {
               "district_id":"101",
               "children":[
                  {
                     "municipalities_id":"asdf",
                     "children":[
                        {
                           "institutes_id":"1",
                           "children":[
                              {

                              }
                           ]
                        }
                     ]
                  }
               ]
            },
            {
               "district_id":"101",
               "children":[
                  {
                     "municipalities_id":"asdf",
                     "children":[
                        {
                           "institutes_id":"2",
                           "children":[
                              {

                              }
                           ]
                        }
                     ]
                  }
               ]
            },
            {
               "district_id":"101",
               "children":[
                  {
                     "municipalities_id":"sdfg",
                     "children":[
                        {
                           "institutes_id":"1",
                           "children":[
                              {

                              }
                           ]
                        }
                     ]
                  }
               ]
            },
            {
               "district_id":"101",
               "children":[
                  {
                     "municipalities_id":"sdfg",
                     "children":[
                        {
                           "institutes_id":"2",
                           "children":[
                              {

                              }
                           ]
                        }
                     ]
                  }
               ]
            },
            {
               "district_id":"102",
               "children":[
                  {
                     "municipalities_id":"dfgh",
                     "children":[
                        {
                           "institutes_id":"1",
                           "children":[
                              {

                              }
                           ]
                        }
                     ]
                  }
               ]
            },
            {
               "district_id":"102",
               "children":[
                  {
                     "municipalities_id":"dfgh",
                     "children":[
                        {
                           "institutes_id":"2",
                           "children":[
                              {

                              }
                           ]
                        }
                     ]
                  }
               ]
            }
         ]
      },
      {
         "counties_id":"2",
         "children":[
            {
               "district_id":"202",
               "children":[
                  {
                     "municipalities_id":"fghj",
                     "children":[
                        {
                           "institutes_id":"1",
                           "children":[
                              {

                              }
                           ]
                        }
                     ]
                  }
               ]
            },
            {
               "district_id":"202",
               "children":[
                  {
                     "municipalities_id":"fghj",
                     "children":[
                        {
                           "institutes_id":"2",
                           "children":[
                              {

                              }
                           ]
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

And this is what I want:

{
   "counties":[
      {
         "counties_id":"1",
         "counties_h":"Megye 1",
         "counties_en":"Coun 1",
         "districts":[
            {
               "district_id":"101",
               "district_h":"Korz 1",
               "district_en":"Dist 1",
               "municipalities":[
                  {
                     "municipalities_id":"asdf",
                     "municipalities_h":"Onk 1",
                     "municipalities_en":"Mun 1",
                     "institutes":[
                        {
                           "institutes_id":"1",
                           "institutes_h":"Int 1",
                           "institutes_en":"Inst 1"
                        },
                        {
                           "institutes_id":"2",
                           "institutes_h":"Int 2",
                           "institutes_en":"Inst 2"
                        }
                     ]
                  },
                  {
                     "municipalities_id":"sdfg",
                     "municipalities_h":"Onk 2",
                     "municipalities_en":"Mun 2",
                     "institutes":[
                        {
                           "institutes_id":"3",
                           "institutes_h":"Int 1",
                           "institutes_en":"Inst 1"
                        },
                        {
                           "institutes_id":"4",
                           "institutes_h":"Int 2",
                           "institutes_en":"Inst 2"
                        }
                     ]
                  }
               ]
            },
            {
               "district_id":"102",
               "district_h":"Korz 2",
               "district_en":"Dist 2",
               "municipalities":[
                  {
                     "municipalities_id":"dfgh",
                     "municipalities_h":"Onk 1",
                     "municipalities_en":"Mun 1",
                     "institutes":[
                        {
                           "institutes_id":"5",
                           "institutes_h":"Int 1",
                           "institutes_en":"Inst 1"
                        },
                        {
                           "institutes_id":"6",
                           "institutes_h":"Int 2",
                           "institutes_en":"Inst 2"
                        }
                     ]
                  }
               ]
            }
         ]
      },
      {
         "counties_id":"2",
         "counties_h":"Megye 2",
         "counties_en":"Coun 2",
         "districts":[
            {
               "district_id":"202",
               "district_h":"Korz 2",
               "district_en":"Dist 2",
               "municipalities":[
                  {
                     "municipalities_id":"fghj",
                     "municipalities_h":"Onk 1",
                     "municipalities_en":"Mun 1",
                     "institutes":[
                        {
                           "institutes_id":"7",
                           "institutes_h":"Int 1",
                           "institutes_en":"Inst 1"
                        },
                        {
                           "institutes_id":"8",
                           "institutes_h":"Int 2",
                           "institutes_en":"Inst 2"
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

回答1:


Check out the shape-json module on NPM.

It will make your life much simpler.



来源:https://stackoverflow.com/questions/29921218/node-js-convert-flat-json-to-hierarchical-json-without-parent-child-attrib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!