Issue
How to only get first object of an array from API call and transform the first object of the array according to myModel and pass the object to the assembleMyModel function in RxJS?
API call returns more than one object in an array. I need the first object.
I am able to transform the data but I am transforming the whole array. I need to transform the first object and then pass the transformed object to assembleMyModel function.
API sample data:
data: [
{
"vehicleYear": 2016,
"vehiclemake": "zzz",
"vehicleeColor": "Red"
},
{
"vehicleYear": 2017,
"vehiclemake": "zzz",
"vehicleeColor": "Blue"
},
{
"vehicleYear": 2018,
"vehiclemake": "zzz",
"vehicleeColor": "Blue"
}
]
Solution
I think away to achieve what you want could be the following
getData$(model: string): Observable<myModel> {
return this.http
.get(`${this.path/model=${model}`).pipe(
map((resp) => resp.data[0] as Vehicle),
map(firstVehicle => assembleMyModel(firstVehicle))
)
}
Here a stackblitz to see the code with http call simulated).