title: How to cast all nodes of a nested JSON tree to instances of a class in angular 2 Typescript?
body: <p>I have a class named Leaf, and I'd like to make all nodes of a json response instances of Leaf.
  The json response looks like this:</p>
  
  <p><strong>JSON Response</strong></p>
  
  <pre><code>{
      "name":"animal",
      "state":false,
      "children":[
          {
              "name":"cats",
              "state":false,
              "children":[
                  {
                      "name":"tiger",
                      "state":false,
                  },
                  {
                      "name":"puma",
                      "state":false,
                      "children":[
                          {
                              "name":"babyPuma",
                              "state":true
                          }
                      ]
                  }
              ]
          },
          {
              "name":"dogs",
              "state":false,
              "children":[
                  {
                      "name":"pitbull",
                      "state":true
                  },
                  {
                      "name":"german",
                      "state":false,
                      "children":[
                          {
                              "name":"shepherd",
                              "state":true
                          }
                      ]
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>I am getting the response using observable and have learned to cast the first node:</p>
  
  <p><strong>http.service.ts snippet</strong></p>
  
  <pre><code>getTreeData(): Observable&lt;Leaf&gt;{  
      var headers = new Headers();
      headers.append('Content-Type', 'application/json');
      return this.http.post('http://localhost:8000/',{ headers: headers ...})
          .map(res =&gt;new Leaf(res.json()));//maybe I could do something here?
  }
  </code></pre>
  
  <p>and here is my Leaf class just in case:</p>
  
  <p><strong>Leaf.ts</strong></p>
  
  <pre><code>export class Leaf{
      name: string;
      state: string;
      treeData: Array&lt;Leaf&gt;;
  
  
      constructor(input:any){
          this.name = input.name;
          this.state = input.state;
          this.treeData = input.children;
      }
  
      toggleState() {
          if (this.state=='active'){
              this.state = 'inactive';
          }
          else {
              this.state = 'active'
          }
      }
  }
  </code></pre>
  
  <p>My final goal is to get the json tree and represent them in a folder tree format. Is there a way to use map() to go through all nodes or combination of map() and some sort of traverser?</p>