0

i get with angular service HTTP a json with node children .

 {  
      "id":"CAT0001",
      "parentId":null,
      "label":"Internal Cause",
      "level":1,
      "enabled":true,
      "children":[  
         {  
            "id":"CAT0003",
            "parentId":"CAT0001",
            "label":"Internal Deliberate Act",
            "level":2,
            "enabled":true,
            "children":[  ]
         },
         {  
            "id":"CAT0004",
            "parentId":"CAT0001",
            "label":"Human Error",
            "level":2,
            "enabled":true,
            "children":[  
               {  
                  "id":"CAT0005",
                  "parentId":"CAT0004",
                  "label":"System input error",
                  "level":3,
                  "enabled":true,
                  "children":[  

                  ]
               }

i want use .filter on my observable for keep only node where enabled === true

i think i need to create a recursive fonction or use a good operator rxjs

for the moment i succes to filter juste the firste level

getTreeData(url:string) {
    return this.httpService.get(url).map(data => data.filter(data => data.enabled === true));
  }

somebody can help me ?

PoussinDev
  • 21
  • 4

1 Answers1

1

This isn't really a rxjs question, since it's the resulting object's children you want to filter out.

I created this stackblitz to help visualize the problem https://stackblitz.com/edit/recursive-tree?file=app/tree.service.ts

Where the main problem is solved by doing a recursive typescript function.

export interface TreeNode {
  id: string;
  parentId?: string;
  label: string;
  level: number;
  enabled: boolean;
  children: TreeNode[];
}

filterActiveNodes(nodes: TreeNode[]) {
  const filteredNodes = nodes.filter(n => n.enabled);
  // filter children
  for (const n of filteredNodes) {
    n.children = this.filterActiveNodes(n.children);
  }

  return filteredNodes;
}
Leon Radley
  • 7,596
  • 5
  • 35
  • 54
  • Thx very much for your help :) , i want use rxjx for that for train with obersable, but your are right is more simple with recursive typescript function – PoussinDev Apr 09 '18 at 07:05