I need to extract data from an http get request and assign it to 3 separate variables once fetched.
Data: [
{
reportId: 1,
results1: [{
name: "TotalReferralsMade",
optionId: 3082,
factor: 1,
description: null
}],
results2: [ ],
results3: [{
name: "2014/15",
dateRangeId: 469,
dateFrom: "2014-04-01T00:00:00.000Z",
dateTo: "2015-03-31T00:00:00.000Z"
}]}]
Above is the structure of the data that needs to be split into 3 variables. Below is the code for the http request:
public GetReportOptions (): Observable<ReportOptions[]> {
return this._http.get('uri')
.map((response: Response) => <ReportOptions[]>response.json())
.do(data => console.log("Options: " + JSON.stringify(data)))
.catch(this.handleError);
}
The service is subscribed by a component, which then fetches the data in ngOnChanges function.
getReportOptions(id: number) {
this._Service.GetReportOptions(id)
.subscribe(options => this.results = options }, error => this.errorMessage = <any>error)
}
The retrieved data is currently stored in a single object called results. However, I would like to store them in individual objects named results1, results2, and results3. Is there a way to achieve this?