Created
August 5, 2018 22:18
-
-
Save johnbahamon/7a7d05efcee1d0d5c7ea9ffa11d39b5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.api.get('categories') | |
.subscribe(data => { | |
this.categories = data | |
var indexed_nodes = {}, tree_roots = []; | |
for (var k | |
= 0; k < data.length; k += 1) { | |
data[k].children = []; | |
} | |
for (var i = 0; i < data.length; i += 1) { | |
indexed_nodes[data[i]._id] = data[i]; | |
} | |
for (var j = 0; j < data.length; j += 1) { | |
var parent = data[j].parent; | |
if (parent === undefined) { | |
tree_roots.push(data[j]); | |
} else { | |
indexed_nodes[parent].children.push(data[j]); | |
} | |
} | |
console.log(JSON.stringify(tree_roots, undefined, "\t")); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment