Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using AngularJS and am in my controller. I have my $scope.tree variable to hold a simple JSON object. I am trying to build a nested tree and the data is coming from several rest calls.

After my first REST call, I successfully have my initial $scope.tree object:

[
     {"name":"Item 1 Name","id":"1","parentid":"","children":[]},
     {"name":"Item 2 Name","id":"2","parentid":"","children":[]}
]

I make a second REST call and have 2 elements returned:

{"name":"Item 3 Name","id":"3","parentid":"2","children":[]},
{"name":"Item 4 Name","id":"4","parentid":"2","children":[]} 

and I want to add these as appropriate children to the first call to end up with:

[
     {"name":"Item 1 Name","id":"1","children":[]},
     {"name":"Item 2 Name","id":"2","children":[
          {"name":"Item 3 Name","id":"3","parentid":"2","children":[]},
          {"name":"Item 4 Name","id":"4","parentid":"2","children":[]}
     ]}
]

A third call:

{"name":"Item 5 Name","id":"5","parentid":"3","children":[]},
{"name":"Item 6 Name","id":"6","parentid":"4","children":[]} 

And end up with:

[
     {"name":"Item 1 Name","id":"1","children":[]},
     {"name":"Item 2 Name","id":"2","children":[
          {"name":"Item 3 Name","id":"3","parentid":"2","children":[
              {"name":"Item 5 Name","id":"5","parentid":"3","children":[]}
          ]},
          {"name":"Item 4 Name","id":"4","parentid":"2","children":[
              {"name":"Item 6 Name","id":"6","parentid":"4","children":[]} 
          ]}
     ]}
]

In my controller I have something like:

$scope.tree = {};
$scope.restcall1 = (results of restcall)
$scope.restcall2 = (results of restcall)
$scope.restcall3 = (results of restcall)

$scope.tree = $scope.restcall1;

now I need to join $scope.restcall1 and $scope.restcall2 so I built a treeBuilder service, with an addCall2 method.

$scope.tree = treeBuilder.addBranch($scope.tree, $scope.$restcall1);

In treeBuilder service:

    addBranch: function(branch, tree){
        for (var i=0; i<branch.length; i++){
            network = _.filter(tree, function(obj){
                if(obj.id == branch[i].parentId){
                    obj.children.push(branch[i]);
                }
            });
        }
        return tree;
    },

obj.children.push(new_branch); doesnt work, and I have tried 10 other ways - I am finding the parent object just fine but can't find a way to add the branch to the parent.children. As you see above, I have underscore available to me if that helps.

Thanks!

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

What i basically understood about what you wanna to do is this:

var obj = {
    children:[]
}
obj.children.push({test:'Hello world'});
console.log(obj.children);//[Object { test="Hello world"}]

Am i right?

share|improve this answer
 
Exactly, except once I find my parent object I want to push onto, I do: console.log(obj.children) and get [] (good!) console.log(new_child) and get {"id:"3", "name":"Object 3"...} then: obj.children.push(new_child); Error: obj.children.push is not a function –  MThebert Feb 9 at 22:15
 
ok, but you've to be sure that all of your objects come with children property, you are doing a loop, some objects can pass correctly and some other not. –  Rodrigo Fonseca Feb 9 at 22:30
1  
i think that your obj.children is a string and not an array, that's why it doesn't have a push function. –  Rodrigo Fonseca Feb 9 at 22:42
1  
console.log(typeof obj.children), type it and see. –  Rodrigo Fonseca Feb 9 at 22:44
1  
ok! good! and one tip, you are receiving your data in json format from your rest call, if you wanna manipulate it in javascript is better you to turn your json to a javascript object. –  Rodrigo Fonseca Feb 9 at 23:22
show 2 more comments

Your initial tree:

[
     {"name":"Item 1 Name","children":[]},
     {"name":"Item 2 Name","children":[]}
]

Your second REST call

{"name":"Item 3 Name","id":"3","parentid":"2","children":[]},
{"name":"Item 4 Name","id":"4","parentid":"2","children":[]}

and your code:

addBranch: function(branch, tree){
        for (var i=0; i<branch.length; i++){
            network = _.filter(tree, function(obj){
                if(obj.id == branch[i].parentId){
                    obj.children.push(branch[i]);
                }
            });
        }
        return tree;
    },

two things that i noticed, your initial tree doesn't have a "id" or "parent id", how this initial tree would work with your algorithm above?

your algorithm is "simple", just check if the object(object1) has a "parent id" attribute, if it have, you just hold this object, find in your array of objects(your tree) if an object has (id === parent_id), if true, you just push your object(object1) inside this another object array atribute "children" and after that you just remove your object(object1) from original resource, and look for another object to push in your tree.

share|improve this answer
 
Rodrigo - Sorry I edited my code to make it consistent. I made a few changes as I went trying to make it clearer. and forgot to edit that call with the ID for the matching later. Also - my code does the match, so I have both the right spot in the parent and the child but I cant figure out how to push the child onto the parent. obj.children.push(branch[i]) doesnt work - that is what I am looking for –  MThebert Feb 8 at 22:13
 
console.log(branch[i]); Do that and see what is inside the branch. –  Rodrigo Fonseca Feb 8 at 22:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.