I'm facing a brain-breaker (at leats for me) : I get a json file with a one depth array of data objects like this:
[
{"id":1, "name":"Sport", "parent_id":0, "children":[]},
{"id":2, "name":"Tennis", "parent_id":4, "children":[]},
{"id":3, "name":"Climbing", "parent_id":5, "children":[]},
{"id":4, "name":"Indoor", "parent_id":1, "children":[]},
{"id":5, "name":"Outdoor", "parent_id":1, "children":[]},
{"id":6, "name":"Bowling", "parent_id":4, "children":[]}
]
how can I convert this in a tree-structure where children are put inside their parent's children array? objects are not always in te right order, a child can come before its parent in the array. (like id 2 and 3 in my example)
This is how I need it in the end:
[
{"id":1, "name":"Sport", "parent_id":0, "children":
[
{"id":4, "name":"Indoor", "parent_id":1, "children":
[
{"id":2, "name":"Tennis", "parent_id":4, "children":[]},
{"id":6, "name":"Bowling", "parent_id":4, "children":[]},
]},
{"id":5, "name":"Outdoor", "parent_id":1, "children":
[
{"id":3, "name":"Climbing", "parent_id":5, "children":[]}
]},
]}
]
Any idea how to achieve this?
I tried iterating over the elements and pushing the inside their parents children array, but when a parent has been moved, the next sibling can't find the parent anymore...
I'm not sure why everyone is so keen on writing inefficient O 2 algorithms (nested for-loops), but here is one in O(n log n) time:
function treeify(nodes) {
var indexed_nodes = {}, tree_roots = [];
for (var i = 0; i < nodes.length; i += 1) {
indexed_nodes[nodes[i].id] = nodes[i];
}
for (var i = 0; i < nodes.length; i += 1) {
var parent_id = nodes[i].parent_id;
if (parent_id === 0) {
tree_roots.push(nodes[i]);
} else {
indexed_nodes[parent_id].children.push(nodes[i]);
}
}
return tree_roots;
}
var nodes = [
{"id":1, "name":"Sport", "parent_id":0, "children":[]},
{"id":2, "name":"Tennis", "parent_id":4, "children":[]},
{"id":3, "name":"Climbing", "parent_id":5, "children":[]},
{"id":4, "name":"Indoor", "parent_id":1, "children":[]},
{"id":5, "name":"Outdoor", "parent_id":1, "children":[]},
{"id":6, "name":"Bowling", "parent_id":4, "children":[]}
];
console.log(JSON.stringify(treeify(nodes), undefined, "\t"));
This should do http://jsfiddle.net/arPgr/
var arr = [
{"id":1, "name":"Sport", "parent_id":0, "children":[]},
{"id":2, "name":"Tennis", "parent_id":4, "children":[]},
{"id":3, "name":"Climbing", "parent_id":5, "children":[]},
{"id":4, "name":"Indoor", "parent_id":1, "children":[]},
{"id":5, "name":"Outdoor", "parent_id":1, "children":[]},
{"id":6, "name":"Bowling", "parent_id":4, "children":[]}
]
var i = 0, len = arr.length, item, parent, id, key, treeArr = {};
for (; i < len; i++) {
item = arr[i];
parent = item.parent_id;
id = item.id;
if (treeArr[id] == undefined) {
treeArr[id] = item;
} else if (typeof treeArr[id] == 'object') {
for (key in item) {
if (key == 'children') continue;
treeArr[id][key] = item[key];
}
}
if (typeof treeArr[parent] != 'object') {
treeArr[parent] = { children: [] };
}
treeArr[parent].children.push(treeArr[id]);
}
console.log(treeArr[0]);
Try this
for (var i = 0; i < a.length; i++) {
for (var j = 0; j < a.length; j++) {
if(a[j].id == a[i].parent_id){
a[i].children.push(a[j])
}
}
}
The function reorder will convert your array to an hierarchy. It returns the object with parent_id ===0 as root object. see this fiddle
var src =
[
{"id":1, "name":"Sport", "parent_id":0, "children":[]},
{"id":2, "name":"Tennis", "parent_id":4, "children":[]},
{"id":3, "name":"Climbing", "parent_id":5, "children":[]},
{"id":4, "name":"Indoor", "parent_id":1, "children":[]},
{"id":5, "name":"Outdoor", "parent_id":1, "children":[]},
{"id":6, "name":"Bowling", "parent_id":4, "children":[]}
];
function reorder(orig)
{
var cnt,id, pid, root, fcnt;
for(cnt = 0; cnt < orig.length; cnt = cnt + 1) {
pid = orig[cnt].parent_id;
id = orig[cnt].id;
if (pid === 0) {
root = orig[cnt]
}
// loop until cnt, then skip that index
for (fcnt=0;fcnt<cnt;fcnt=fcnt+1){
if (orig[fcnt].parent_id === id) {
orig[cnt].children.push(orig[fcnt]);
}
}
// loop from cnt + 1 till end of array
for (fcnt=cnt+1;fcnt<orig.length;fcnt=fcnt+1){
if (orig[fcnt].parent_id === id) {
orig[cnt].children.push(orig[fcnt]);
}
}
}
return root;
}
var dest = reorder(src);
console.log(JSON.stringify(dest));
Use a new array to store the result..
var ret = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i].parent_id == 0) {
ret.push(arr[i]);
continue;
}
for (var j = 0; j < arr.length; j++) {
if (arr[i].parent_id == arr[j].id) {
arr[j].children.push(arr[i]);
break;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.