Sunday, August 20, 2017

Make sure an array is empty instead of just not there when hydrating objects with Observables.

whatever() {
   this.http.get('http://www.example.com')
      .map(
         (response: Response) => {
            const cats: Cat[] = response.json();
            for (let cat of cats) {
               if(!cat['kittens']) {
                  cat['kittens'] = [];
               }
            }
            return cats;
         }
      }
      .subscribe(
         (cats: Cat[]) => {
            this.myService.setCats(cats);
         }
      }
   }
}

No comments:

Post a Comment