javascript - json format of countries data to hierarchal data -
i have json data 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'} ]
i have convert hierarchal structure like..
data:{'uk':{ "whitefield":{ "eastward":{ "eastcounty1":["privatecompany","privatecompany2"], "eastcounty2":["privatecompany3"] }, "westward":{ "westcounty1":["privatecompany"], "eastcounty1":["privatecompany"] } }, "blackfield":{ "westward":{"eastcounty1":["privatecompany"]} } }, "india":{ "andhra":{ "ramnagar":["privatecompany"] } }}
in above case have county=" " or county=null in 1 scenario been replaced shown above in ramnagar ward.
you need loop through each item , build json object fiddle here
function mergedata(data) { var outp = {}; // loop through each object , add (var = 0; < 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>
Comments
Post a Comment