Reputation: 9
I am using the below to try and convert an array with many columns into an array with 2 columns.
I can see when I do a quick watch that res contains an array called result, but i it is not mapping into the new array.
loadData() {
this.selectprojectfromfavoritesService.getFavorites()
.subscribe(res => {
//this.TempArray.push({ label: res.result.projectNumber, value: res.result.tzeroprojectCaptureId })
const TempArray = res.result.map(result => { return { label: result.projectNumber, value: result.tzeroprojectCaptureId }; });
this.values = this.TempArray ;
});
Upvotes: 0
Views: 396
Reputation: 1746
You've made a mistake while getting access to TempArray
value. It's block scoped variable (no class property) so you shouldn't use this
keyword. Here's working example:
loadData() {
this.selectprojectfromfavoritesService.getFavorites()
.subscribe(res => {
const TempArray = res.result.map(result => { return { label: result.projectNumber, value: result.tzeroprojectCaptureId }; });
this.values = TempArray;
});
}
BTW: it would much better (regarding data-flow management and optimization) if you'd use RxJS map
operator to achieve this:
loadData() {
this.selectprojectfromfavoritesService.getFavorites()
.map(res => {
return res.result.map(result => {
return { label: result.projectNumber, value: result.tzeroprojectCaptureId };
})
})
.subscribe(data => this.values = data)
}
Upvotes: 1