json格式的国家数据到层次数据
json format of countries data to hierarchal data
我有这个json数据data:[
{'country':'uk','district':'whitefield','ward':'eastward','county':'eastcounty1','company':'privatecompany'},
{'country':'uk','district':'whitefield','ward':'eastward','county':'eastcounty1','company':'privatecompany2'},
{'country':'uk','district':'whitefield','ward':'eastward','county':'eastcounty2','company':'privatecompany3'},
{'country':'uk','district':'whitefield','ward':'westward','county':'westcounty1','company':'privatecompany'},
{'country':'uk','district':'blackfield','ward':'westward','county':'eastcounty1','company':'privatecompany'},
{'country':'india','district':'andhra','ward':'ramnagar','county':'','company':'privatecompany'}
]
我必须将其转换为层次结构,如。。
data:{'uk':{
"whitefield":{
"eastward":{
"eastcounty1":["privateCompany","privateCompany2"],
"eastcounty2":["privatecompany3"]
},
"westward":{
"westcounty1":["privatecompany"],
"eastcounty1":["privatecompany"]
}
},
"blackfield":{
"westward":{"eastcounty1":["privatecompany"]}
}
},
"india":{
"andhra":{
"ramnagar":["privatecompany"]
}
}}
在上面的情况下,我在一个场景中有country="或country=null,所以它被替换为如上所示的ramnagar病房。
您只需要遍历每个项目并在此处构建json对象Fiddle
function mergeData(data) {
var outp = {};
// loop through each object and add
for (var i = 0; i < data.length; i++) {
var obj = data[i];
if (outp.hasOwnProperty(obj.country)) {
if (outp[obj.country].hasOwnProperty(obj.district)) {
if (outp[obj.country][obj.district].hasOwnProperty(obj.ward)) {
if (outp[obj.country][obj.district][obj.ward].hasOwnProperty(obj.county)) {
outp[obj.country][obj.district][obj.ward][obj.county].push(obj.company);
}
else {
if (obj.county == "" || obj.county == null) {
if (Array.isArray(outp[obj.country][obj.district][obj.ward])) {
outp[obj.country][obj.district][obj.ward].push(obj.company);
}
else {
outp[obj.country][obj.district][obj.ward] = [obj.company];
}
}
else {
outp[obj.country][obj.district][obj.ward][obj.county] = [obj.company];
}
}
}
else {
outp[obj.country][obj.district][obj.ward] = {};
outp[obj.country][obj.district][obj.ward][obj.county] = [obj.company];
}
} else {
outp[obj.country][obj.district] = {};
outp[obj.country][obj.district][obj.ward] = {};
outp[obj.country][obj.district][obj.ward][obj.county] = [obj.company];
}
} else {
outp[obj.country] = {};
outp[obj.country][obj.district] = {};
if (obj.county == "") {
outp[obj.country][obj.district][obj.ward] = [obj.company];
}
else {
outp[obj.country][obj.district][obj.ward] = {};
outp[obj.country][obj.district][obj.ward][obj.county] = [obj.company];
}
}
}
console.log(JSON.stringify(outp));
return outp;
}
function doMerge() {
var data = [{
'country': 'uk',
'district': 'whitefield',
'ward': 'eastward',
'county': 'eastcounty1',
'company': 'privatecompany'
}, {
'country': 'uk',
'district': 'whitefield',
'ward': 'eastward',
'county': 'eastcounty1',
'company': 'privatecompany2'
}, {
'country': 'uk',
'district': 'whitefield',
'ward': 'eastward',
'county': 'eastcounty2',
'company': 'privatecompany3'
}, {
'country': 'uk',
'district': 'whitefield',
'ward': 'westward',
'county': 'westcounty1',
'company': 'privatecompany'
}, {
'country': 'uk',
'district': 'blackfield',
'ward': 'westward',
'county': 'eastcounty1',
'company': 'privatecompany'
}, {
'country': 'india',
'district': 'andhra',
'ward': 'ramnagar',
'county': '',
'company': 'privatecompany'
}];
mergeData(data);
}
<button onclick="doMerge()">Merge</button>
- 用程序搜索JQuery数据表中的文本
- 要求输入在数据列表中
- 正在将数据主题添加到所有项目
- 函数参数中的数据与指定变量之间的任何性能差异
- 在VanillaJS中模拟模型双向数据绑定
- CSS-如何定位内容数据标题
- 使用电话间隙在Android应用程序中显示SQL Lite的数据
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- 从json对象聚集数据并创建层次结构
- json格式的国家数据到层次数据
- D3:使用 nest 函数将带有父键的平面数据转换为层次结构
- 如何在node.js应用程序中的mongodb中按层次结构顺序插入数据
- 角度选择/下拉层次数据
- 根据行数据创建层次结构
- 多级网格层次结构-祖父母数据
- 用于报告层次结构数据的下拉列表
- KendoTreeView在第二层次结构中需要什么数据格式?
- 使用HTML和CSS可视化数据层次结构
- 如何在层次网格中为嵌套的剑道UI数据源过滤数据
- 如何将这种原始json数据转换/分组为层次结构