I have a API which return JSON array of objects. Which need to be converted into nested JSON array in a parent/child relation based on "parientid" in each node. Where "parientid" is null it will be root nodes and other will be child node of there parent node. Below is the sample JSON array which I get from API.
var API_DATA = [
{
"DIAGID": 1,
"DIAGNOSIS": "Certain infectious or parasitic diseases ",
"DIAGTYPE": "Chapter",
"PARENTID": null,
},
{
"DIAGID": 2,
"DIAGNOSIS": "Gastroenteritis or colitis of infectious origin ",
"DIAGTYPE": "Section",
"PARENTID": 1,
},
{
"DIAGID": 3,
"DIAGNOSIS": "Bacterial intestinal infections",
"DIAGTYPE": "Category",
"PARENTID": 2,
},
{
"DIAGID": 4,
"DIAGNOSIS": "Cholera",
"DIAGTYPE": "Group",
"PARENTID": 3,
},
{
"DIAGID": 5,
"DIAGNOSIS": "Intestinal infection due to other Vibrio",
"DIAGTYPE": "Disease",
"PARENTID": 4,
},
{
"DIAGID": 6,
"DIAGNOSIS": "Intestinal infections due to Shigella",
"DIAGTYPE": "Disease",
"PARENTID": 4,
},
{
"DIAGID": 7,
"DIAGNOSIS": "Intestinal infections due to Escherichia coli",
"DIAGTYPE": "Disease",
"PARENTID": 4,
},
{
"DIAGID": 8,
"DIAGNOSIS": "Neoplasms",
"DIAGTYPE": "Chapter",
"PARENTID": null,
},
{
"DIAGID": 9,
"DIAGNOSIS": "Neoplasms of brain or central nervous system",
"DIAGTYPE": "Section",
"PARENTID": 8,
},
{
"DIAGID": 10,
"DIAGNOSIS": "Primary neoplasms of brain ",
"DIAGTYPE": "Category",
"PARENTID": 9,
},
{
"DIAGID": 11,
"DIAGNOSIS": "Gliomas of brain",
"DIAGTYPE": "Group",
"PARENTID": 10,
},
{
"DIAGID": 12,
"DIAGNOSIS": "Glioblastoma of brain",
"DIAGTYPE": "Disease",
"PARENTID": 11,
},
{
"DIAGID": 13,
"DIAGNOSIS": "Other specified gliomas of brain",
"DIAGTYPE": "Disease",
"PARENTID": 11,
}
]
I want to convert it into tree format in parent/child relation based on "ParentId" in each object. Below is the example of JSON in which I want to convert it. I want "DIAGID" as value and "DIAGNOSIS" as name.
const data = {
label: 'search me',
value: 'searchme',
children: [
{
label: 'search me too',
value: 'searchmetoo',
children: [
{
label: 'No one can get me',
value: 'anonymous',
},
],
},
],
}
I am new to javascript I have used javascript array, map function but need little guide on what approach should i follow.
You could take a standard approach for trees with a single loop.
var data = [{ DIAGID: 1, DIAGNOSIS: "Certain infectious or parasitic diseases ", DIAGTYPE: "Chapter", PARENTID: null }, { DIAGID: 2, DIAGNOSIS: "Gastroenteritis or colitis of infectious origin ", DIAGTYPE: "Section", PARENTID: 1 }, { DIAGID: 3, DIAGNOSIS: "Bacterial intestinal infections", DIAGTYPE: "Category", PARENTID: 2 }, { DIAGID: 4, DIAGNOSIS: "Cholera", DIAGTYPE: "Group", PARENTID: 3 }, { DIAGID: 5, DIAGNOSIS: "Intestinal infection due to other Vibrio", DIAGTYPE: "Disease", PARENTID: 4 }, { DIAGID: 6, DIAGNOSIS: "Intestinal infections due to Shigella", DIAGTYPE: "Disease", PARENTID: 4 }, { DIAGID: 7, DIAGNOSIS: "Intestinal infections due to Escherichia coli", DIAGTYPE: "Disease", PARENTID: 4 }, { DIAGID: 8, DIAGNOSIS: "Neoplasms", DIAGTYPE: "Chapter", PARENTID: null }, { DIAGID: 9, DIAGNOSIS: "Neoplasms of brain or central nervous system", DIAGTYPE: "Section", PARENTID: 8 }, { DIAGID: 10, DIAGNOSIS: "Primary neoplasms of brain ", DIAGTYPE: "Category", PARENTID: 9 }, { DIAGID: 11, DIAGNOSIS: "Gliomas of brain", DIAGTYPE: "Group", PARENTID: 10 }, { DIAGID: 12, DIAGNOSIS: "Glioblastoma of brain", DIAGTYPE: "Disease", PARENTID: 11 }, { DIAGID: 13, DIAGNOSIS: "Other specified gliomas of brain", DIAGTYPE: "Disease", PARENTID: 11 }],
tree = function (data, root) {
var t = {};
data.forEach(({ DIAGID, DIAGNOSIS, PARENTID }) => {
Object.assign(t[DIAGID] = t[DIAGID] || {}, { label: DIAGID, name: DIAGNOSIS });
t[PARENTID] = t[PARENTID] || {};
t[PARENTID].children = t[PARENTID].children || [];
t[PARENTID].children.push(t[DIAGID]);
});
return t[root].children;
}(data, null);
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }