2015-04-28 45 views
1

我想从我的sql查询中构建分层数据结构。 继this post之后从平面对象中构建分层JSON对象后,我尝试在对象层次中创建具有更多属性的4级层次结构,但不需要子属性。Node.js - 将平面json转换为没有'parent','child'属性的层次结构json

我怎样才能做到这一点?

这里是我的JavaScript(的NodeJS)代码:

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 }); 
     } 
    }); 
}); 

第一级是确定的,因为这equalation检查相同的第一级别属性:

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

我怎样才能chect二路,3TH和第四级相同的情况?

这是平坦的对象,它一直努力:我目前的代码

[ 
{ 
"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" 
} 
] 

输出:

{ 
    "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":[ 
           { 

           } 
          ] 
         } 
        ] 
        } 
       ] 
      } 
     ] 
     } 
    ] 
} 

而这正是我想要的:

{ 
    "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" 
         } 
        ] 
        } 
       ] 
      } 
     ] 
     } 
    ] 
} 
+0

JSON应该在服务器中格式化并发送到客户端。你正在尝试的方法是一个不好的做法。 –

+0

这个JSON格式在服务器端(使用NodeJS)和我将发送给客户端的输出。 – lgabster

回答