I'm trying to transform my data on client side from this format:
let testLoad= [{"id":7,"name":"Kuwait","parentId":2},
 {"id":4,"name":"Iraq","parentId":2},
 {"id":10,"name":"Qatar","parentId":2},
 {"id":2,"name":"Middle East","parentId":1},
 {"id":3,"name":"Bahrain","parentId":2},
 {"id":6,"name":"Jordan","parentId":2},
 {"id":8,"name":"Lebanon","parentId":2},
 {"id":1,"name":"Africa/Middle East","parentId":null},
 {"id":5,"name":"Israel","parentId":2},
 {"id":9,"name":"Oman","parentId":2}];
to this format:
 let testLoad= [{"id":55,"text":"Africa/Middle East","children":[
 {"id":2,"text":"Middle East","children": [{"id":7,"name":"Kuwait","children":[]},
{"id":4,"name":"Iraq","children":[]},
{"id":10,"name":"Qatar","children":[]},
{"id":3,"name":"Bahrain","children":[]},
{"id":6,"name":"Jordan","children":[]},
{"id":8,"name":"Lebanon","children":[]},
{"id":5,"name":"Israel","children":[]},
{"id":9,"name":"Oman","children":[]}]}]
so I can use it in a tree library like gijgo tree or jstree in javascript.
- 
 Possible duplicate of How to efficiently build a tree from a flat structure?Denis Chevalier– Denis Chevalier2017年03月23日 17:14:30 +00:00Commented Mar 23, 2017 at 17:14
- 
 Please provide some code you've written till now to do this.ayushgp– ayushgp2017年03月23日 17:28:13 +00:00Commented Mar 23, 2017 at 17:28
- 
 Also refer to this link for further questions as this may help you. stackoverflow.com/help/how-to-askSatish Prakash Garg– Satish Prakash Garg2017年03月23日 17:33:57 +00:00Commented Mar 23, 2017 at 17:33
4 Answers 4
You could use recursion :
var testLoad= [{"id":7,"name":"Kuwait","parentId":2},
 {"id":4,"name":"Iraq","parentId":2},
 {"id":10,"name":"Qatar","parentId":2},
 {"id":2,"name":"Middle East","parentId":1},
 {"id":3,"name":"Bahrain","parentId":2},
 {"id":6,"name":"Jordan","parentId":2},
 {"id":8,"name":"Lebanon","parentId":2},
 {"id":1,"name":"Africa/Middle East","parentId":null},
 {"id":5,"name":"Israel","parentId":2},
 {"id":9,"name":"Oman","parentId":2}];
function lookingForNodeWithParent( nodes, parentId ) {
 var arrayToReturn = [];
 for( var i = 0, length = nodes.length; i < length; i++ ) {
 if( nodes[i].parentId === parentId ) {
 var node = nodes[i];
 
 arrayToReturn.push({
 id: node.id,
 name: node.name,
 children: lookingForNodeWithParent( nodes, node.id )
 });
 }
 }
 
 return arrayToReturn;
}
var array = lookingForNodeWithParent( testLoad, null );
console.log( array )1 Comment
The easiest way would be to create a map of the items based on their ID.
Example: var objectMap = {5: {"name":"Jordan","children":[], parentId: '2'}}
Then you can iterate on them to build your structure:
for (key in objectMap) {
 var object = objectMap[key];
 var parent = objectMap[parseInt(object.parentId)];
 parent.children.push(object);
}
2 Comments
You could collect all nodes an built part trees out of it and get the root note for the tree of the collecting object.
- It works in a single loop. 
- It works for unsorted data. 
- It builds for any node a node with the given data and takes the parts from the node as parent and a parent node if not exist and put it as a children to it. 
var data = [{ id: 7, name: "Kuwait", parentId: 2 }, { id: 4, name: "Iraq", parentId: 2 }, { id: 10, name: "Qatar", parentId: 2 }, { id: 2, name: "Middle East", parentId: 1 }, { id: 3, name: "Bahrain", parentId: 2 }, { id: 6, name: "Jordan", parentId: 2 }, { id: 8, name: "Lebanon", parentId: 2 }, { id: 1, name: "Africa/Middle East", parentId: null }, { id: 5, name: "Israel", parentId: 2 }, { id: 9, name: "Oman", parentId: 2 }],
 tree = function (data, root) {
 var r = [], o = {};
 data.forEach(function (a) {
 a.children = o[a.id] && o[a.id].children || [];
 o[a.id] = a;
 if (a.parentId === root) {
 r.push(a);
 } else {
 o[a.parentId] = o[a.parentId] || {};
 o[a.parentId].children = o[a.parentId].children || [];
 o[a.parentId].children.push(a);
 }
 });
 return r;
 }(data, null);
console.log(tree);.as-console-wrapper { max-height: 100% !important; top: 0; }2 Comments
Yes. You can see an example at http://gijgo.com/tree/demos/bootstrap-treeview. Click on the "Back-End Code" tab in order to see how to do that with Linq or EF in .NET