Node.js-将平面json转换为没有'parent','child'属性的分层json [英] Node.js - Convert flat json to hierarchical json without 'parent','child' attributes
本文介绍了Node.js-将平面json转换为没有'parent','child'属性的分层json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从我的sql查询中构建分层数据结构. 在遵循这篇文章之后为了从平面对象构建层次结构JSON对象,我尝试创建4级层次结构,该层次结构在对象级别具有更多属性,但是不需要children属性.
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.
我该怎么做?
这是我的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 });
}
});
});
第一级还可以,因为此等式检查相同的第一级属性:
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?
这是不断尝试的扁平对象:
[
{
"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"
}
]
}
]
}
]
}
]
}
推荐答案
查看 shape-json模块在NPM上.
它将使您的生活变得更加简单.
It will make your life much simpler.
这篇关于Node.js-将平面json转换为没有'parent','child'属性的分层json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文