0

I want remove the particular element from nested json array. The below json object has root node as EE with nested child Nodes & Packages. I want to delete the node id = 7. Is it possible to do that?

$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 
       }]
};
Richard
  • 6,812
  • 5
  • 45
  • 60

1 Answers1

0

You could use a library such as lodash to remove the unwanted items:

 var array = [1, 2, 3, 4];
var evens = _.remove(array, function(n) {
  return n % 2 == 0;
});

console.log(array);
// → [1, 3]

console.log(evens);
// → [2, 4]

lodash remove documentation

Turkster
  • 46
  • 4
  • Thanks for the reply. Basically, this is like tree structure with childnodes and packages as different icons in front end. I want to delete dynamically based on selected node id by external button. – Prabakar Jul 05 '16 at 07:49
  • shouldnt be an issue, create a function that takes the id as a param and you can then pass it to lodash to filter out the selected item. With the binding in Angular your UI will be updated automatically. Lodash supports filtering of sub collections so it should be a perfect fit. Here is an example of what I mean: http://stackoverflow.com/questions/17096988/lodash-how-do-i-use-filter-when-i-have-nested-object – Turkster Jul 05 '16 at 08:02