从angularjs中的多嵌套Json数组对象中删除元素

时间:2016-07-05 07:02:31

标签: angularjs json

我想从嵌套的json数组中删除特定元素。 下面的json对象将根节点作为EE,具有嵌套的子节点&包。我想删除节点id = 7。有可能吗?

$scope.data = { 
       "id": 1, "parentNodeId": null, 
       "nodeName": "EE",  "magCode": "EE", 
       "childNodes": [{ 
              "id": 2, 
              "parentNodeId": 1, 
              "nodeName": "Child 1", 
              "magCode": "EE", 
              "childNodes": [], 
              "packages": [] 
           }, 
           { 
              "id": 4, 
              "parentNodeId": 1, 
              "nodeName": "Child 2", 
              "magCode": "EE", 
              "childNodes": [{ 
                     "id": 5, 
                     "parentNodeId": 4, 
                     "nodeName": "Child 21", 
                     "magCode": "EE", 
                     "childNodes": [], 
                     "packages": [] }], 
              "packages": [{ 
                     "id": 6, 
                     "parentNodeId": 4, 
                     "nodeName": "Child Package 2", 
                     "magCode": "EE", 
                     "childNodes": null, 
                     "packages": null 
                   },
                   { 
                     "id": 7, 
                     "parentNodeId": 4, 
                     "nodeName": "Child Package 3", 
                     "magCode": "EE", 
                     "childNodes": null, 
                     "packages": null 
                   }
                ]
       }],
       "packages": [{ 
             "id": 8, 
             "parentNodeId": 1, 
             "nodeName": "test", 
             "magCode": "EE", 
             "childNodes": null, 
             "packages": null 
       }]
};

1 个答案:

答案 0 :(得分:0)

您可以使用lodash等库来删除不需要的项目:

this.filterForm = new FormGroup({
  title: new FormControl('', Validators.required)
});

lodash remove documentation