I'm trying to use rxjs map to get an array of strings from a response.
The response is an array of objects with guid and name property.
When i get the full response:
public getAllSolutions(): Observable<string[]> {
return this.http.get(`${this.baseUrl}/Solution/All`)
.map((results: Response) => results.json())
}
and output it using:
this.solutionsService.getAllSolutions().subscribe(
data => {
console.log(data);
}
);
i get this:
[
{guid:"6e3d4646e1ad6d78bd225d2bdb5a14709c12e8280796a3b4f27536e8aaaf89ed", name: "Solution 1"},
{guid: "737f838cc457d833ff1dc01980aa56e9661304a26e33885defe995487e3306e7", name: "Solution 2"}
]
What i would like to have is an array just containing
name
Following documentation this should work:
public getAllSolutions(): Observable<string[]> {
return this.http.get(`${this.baseUrl}/Solution/All`)
.map((results: Response) => results.json())
.map(solutions => solutions.name);
}
but the output i get is undefined. What am i doing wrong here?
.map(solutions => solutions.map(s => s.name));mapapplies to then? Isn't this first map already applied to the result array, as there is nothing else there? Or is the .json() result an object that contains an array?results.json()results in array (as you can see in console.log). So you could just applyArray.mapin place, instead of doing it in second map on observable:.map((results: Response) => results.json().map(s => s.name)). I think it will be helpful to read about the difference betweenArray.mapandObservable.map.