At one point I was successfully iterating over objects in an array in my Angular 2 app using observables. In my client service file I had this:
getByCategory(category: string) {
const q = encodeURIComponent(category);
return this.http.get
(`https://api.someurl.com/${this.ver}/clients/category/${q}?apikey=${this.key}`)
.map((response: Response) => response.json())
.catch(this.stageErrorsHandler);
}
stageErrorsHandler(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
I then I was subscribing in my component's ngOnInit life cycle hook, like this:
ngOnInit() {
this.clientService.getByCategory('consulting')
.subscribe(resRecordsData => this.records = resRecordsData,
responseRecordsError => this.errorMsg = responseRecordsError);
}
In the component, I also initiated records to be an empty array:
records = [];
And then in my view I was iterating over this array of records like this:
<tr *ngFor="let record of records">
Now the configuration of our data on the api/server has changed. Where I used to be targeting an array, that looked like this:
[
{
"id": "someid",
}
]
... I need to change this to now target an array called "data", that is located WITHIN an object. It looks like this:
{
"count": 1000,
"data": [
{
"id": "someid",
}
]
}
Simple question that I'm stumped over: How do I target an array WITHIN an object like this? I need to target the array within the object, because that's what *ngFor needs in order to be able to iterate over.