AngularJS和JSON - 添加子对象

时间:2014-02-08 20:56:46

标签: javascript json angularjs rest

我正在使用AngularJS并且在我的控制器中。我有我的$ scope.tree变量来保存一个简单的JSON对象。我正在尝试构建一个嵌套树,数据来自几个休息调用。

在我的第一次REST调用之后,我成功拥有了我的初始$ scope.tree对象:

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

我进行了第二次REST调用并返回了2个元素:

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

我希望将这些作为适当的孩子添加到第一个调用中以结束:

[
     {"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":[]}
     ]}
]

第三个电话:

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

最后:

[
     {"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":[]} 
          ]}
     ]}
]

在我的控制器中,我有类似的东西:

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

$scope.tree = $scope.restcall1;

现在我需要加入$ scope.restcall1和$ scope.restcall2,所以我使用addCall2方法构建了一个treeBuilder服务。

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

在treeBuilder服务中:

    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);不起作用,我已经尝试了其他10种方式 - 我发现父对象很好,但找不到将分支添加到parent.children的方法。如上所述,如果有帮助,我可以向我提供下划线。

谢谢!

2 个答案:

答案 0 :(得分:2)

我基本上了解你想要做的是:

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

我是对的吗?

答案 1 :(得分:0)

您的初始树:

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

您的第二次REST呼叫

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

和你的代码:

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;
    },

我注意到的两件事,你的初始树没有“id”或“父ID”,这个初始树如何与你上面的算法一起工作?

  

你的算法是“简单的”,只需检查对象(object1)是否有   “父ID”属性,如果有,你只需抓住这个对象,找到   你的对象数组(你的树),如果一个对象有(id === parent_id),   如果为true,则只需将对象(object1)推入另一个对象中   数组属性“儿童”,之后你只需删除你的   来自原始资源的object(object1),并查找另一个对象   推进你的树。