I have an Angular 2 app which gets some values from an API (which I can't control) using a promise. The JSON returned is like this:
[{"Id":1053},{"Id":1054},{"Id":1103}]
However, my app only needs to consume an array of numbers i.e. what I need to consume is this:
[1053,1054,1103]
Here's the part of my app which gets the data from the API:
recordIds = [];
ngOnInit() {
this._contentService.getAncestorNodes(currentNodeId).then(
(data) => {
data.map(function(record){
console.log('record.Id = ' + record.Id);
});
}
);
}
I've tried adding this.recordIds.push(record.Id)
into the data.map
function to try to build a new object of just the Ids, but I just get the following error:
Cannot read property 'recordIds' of undefined
Question is, how can I take the response from the API and parse it into the desired format?
Many thanks.