I'm relatively new to Angular2 and I am currently grappling with looping through a JSON object retrieved from a GET request.
Here's the JSON object in question:
{
Results: [{
Time: "2017-02-11T08:15:01.000+00:00",
Id: "data-mopdsjkajskda",
AuthorId: "58fSDNJD"
}, {
Time: "2017-03-11T06:23:34.000+00:00",
Id: "data-2371212hjb1",
AuthorId: "43555HHHJ"
}, {
Time: "2017-04-11T07:05:11.000+00:00",
Id: "data-kjskdha22112",
AuthorId: "XDSJKJSDH"
}]
}
An excerpt from my Angular code snippet:
interface res {
Time: string;
Id: string;
AuthorId: string;
}
export class AppComponent {
results: res;
constructor(private _httpservice: HTTPService) {}
this._httpservice.getQuery().subscribe(
data => {
this.results = data.Results
},
error => console.log(error),
() => console.log('Done')
);
}
Though I have successfully received the data, I aim to extract the Ids and store them in an array. In traditional Javascript, I would typically accomplish this as follows:
var ids = [];
for (i = 0; i < data.Results.length; i++) {
ids.push(data.Results[i].Id)
}
After pushing the Ids into the array, it will look like this:
ids = ['data-mopdsjkajskda', 'data-2371212hjb1', 'data-kjskdha22112'];
However, I find myself struggling to replicate this functionality in Angular2. Any assistance on this matter would be highly appreciated!