Convert flat data to hierarchical with inner objects javascript -
i faced common issue converting simple flat data hierarchical. have found multiple topics still can't how convert flat data necessary me hierarchical format
this json
[ { "id": 1, "name": "sponsor", "description": null, "parentid": null }, { "id": 2, "name": "class", "description": null, "parentid": 1 }, { "id": 3, "name": "study", "description": null, "parentid": 2 }, { "id": 4, "name": "site", "description": null, "parentid": 3 } ]
and need format this
[ { "data":{ "id": 1, "name":"sponsor", "description":null, "parentid":"null" }, "children":[ { "data":{ "id": 2, "name":"class", "description":null, "parentid":"1" }, "children":[ { "data":{ "id": 3, "name":"study", "description":null, "parentid":"2" }, "children": [ { "data":{ "id": 4, "name":"site", "description":null, "parentid":"3" } } ] } ] } ] } ]
this function
flattohierarchy(flat) { let roots = []; let = {}; flat.foreach(function (item) { all[item.id] = item }); object.keys(all).foreach(function (id) { let item = all[id]; if (item.parentid === null) { roots.push(item) } else if (item.parentid in all) { let p = all[item.parentid]; if (!('children' in p)) { p.children = [] } p.children.push(item) } }); console.log(roots); return roots }
output
[ { "id": 1, "name": "sponsor", "description": null, "parentid": null, "children": [ { "id": 2, "name": "class", "description": "together", "parentid": 1, "children": [ { "id": 3, "name": "study", "description": "browsing data", "parentid": 2, "children": [ { "id": 4, "name": "site", "description": null, "parentid": 3, "children": [] } ] } ] } ] } ]
i'm pretty close desire result. me fix ?
edited
the right answer provided @someone3 modified code needs
flattohierarchy (flat) { let roots = []; let = {}; let ids = []; flat.foreach(function (item) { let itemid = item.id; let converteditem = function (id) { let newitem = {}; newitem['data'] = id; return newitem; } ; all[itemid] = converteditem(item); ids.push(itemid); }); (let = 0; < ids.length; i++) { let id = ids[i]; let converteditem = all[id]; let parentid = converteditem.data.parentid; if (parentid === null) { roots.push(converteditem); } else if (parentid in all) { let p = all[parentid]; if (!('children' in p)) { p.children = [] } p.children.push(converteditem) } } return roots }
the code below full source code situation. modified , added few lines source code. note code assumes parents inserted tree before children do. if assumption not true code need changed more this.
let flatdata = [ { "id": 1, "name": "sponsor", "description": null, "parentid": null }, { "id": 2, "name": "class", "description": null, "parentid": 1 }, { "id": 3, "name": "study", "description": null, "parentid": 2 }, { "id": 4, "name": "site", "description": null, "parentid": 3 } ]; function convertitem(item) { let newitem = {}; newitem.data = item; return newitem; } function flattohierarchy(flat) { let roots = []; let = {}; let ids = []; flat.foreach(function (item) { let itemid = item.id; let converteditem = convertitem(item); all[itemid] = converteditem; ids.push(itemid); }); // use ids array instead of object maintain previous order. (let = 0; < ids.length; i++) { let id = ids[i]; let converteditem = all[id]; let parentid = converteditem.data.parentid; if (parentid === null) { delete converteditem.data.parentid; delete converteditem.data.id; roots.push(converteditem); } else if (parentid in all) { let p = all[parentid]; if (!('children' in p)) { p.children = [] } delete converteditem.data.parentid; delete converteditem.data.id; p.children.push(converteditem) } }; console.log(roots); return roots } flattohierarchy(flatdata);
we can factor out 2 deletes before push.
Comments
Post a Comment