JSFiddle - React, Tailwind, and code Playground
JavaScript
var data = [
{
"State_Code": "01",
"County_Code": "000",
"State_Abbrv": "AL",
"County_Name": "ALABAMA",
"DATA": "12345"
},
{
"State_Code": "01",
"County_Code": "001",
"State_Abbrv": "AL",
"County_Name": "AUTAUGA COUNTY",
"DATA": "123"
},
{
"State_Code": "01",
"County_Code": "003",
"State_Abbrv": "AL",
"County_Name": "BALDWIN COUNTY",
"DATA": "321"
},
{
"State_Code": "02",
"County_Code": "000",
"State_Abbrv": "AK",
"County_Name": "ALASKA",
"DATA": "98765"
},
{
"State_Code": "02",
"County_Code": "013",
"State_Abbrv": "AK",
"County_Name": "ALEUTIANS EAST BOROU",
"DATA": "456"
}
];
var newData = { name : 'USA', children : [] };
for(var i=0; i<data.length; i++) {
if(data[i].County_Code == "000") {
newData.children.push({ name : data[i].County_Name, state_code : data[i].State_Code, children : [] });
}
}
for(var i=0; i<data.length; i++)
{
if(data[i].County_Code != "000") {
for(var x=0; x<newData.children.length; x++) {
if(data[i].State_Code == newData.children[x].state_code) {
newData.children[x].children.push({ name : data[i].County_Name, data : data[i].DATA });
}
}
}
}
console.log(newData);
document.write(JSON.stringify(newData));