I am trying to generate a tree of flat array , placing sub categories next to the parent category and having hard doing same.
var categories = [
{
name: 'Javascript'
},
{
name: 'jQuery',
parent: 'Javascript'
},
{
name: 'Angular',
parent: 'Javascript'
},
{
name: 'AngularUi',
parent: 'Angular'
}
];
var tree = [];
function getChilds(array,identifier){
return _.filter(array,function(val){
return val.parent == identifier
});
}
function createTree(array){
for(var x=0;x<_.size(array);x++){
tree.push(array[x].name);
var childs = getChilds(array,array[x].name);
if(_.size(childs) > 0){
createTree(childs);
}else{
console.log(tree);
}
}
}
createTree(categories);
<script src="http://ift.tt/OMnhxL"></script>
This is what i have tried so far, and using underscore for little help. Any help will be appreciated .
Aucun commentaire:
Enregistrer un commentaire