I have a service with a function of type
getSummary(id: string, universe: string): Observable<INTsummary[]>
When I call this I only ever want the first item in the INTsummary[]
, so I'm trying to use rxjs first
operator to do this:
loadQuickSummary(){
var obj = this.myService.getSummary(this.user.id, this.user.universe).first();
console.log("getting quick summary");
obj.subscribe(r => {
console.log(`first value ${JSON.stringify(r)}`)
})
}
from reading docs and stackoverflow, this should return an object, but instead I get back an array of INTsummary
's .
How can I get back just the first object in the INTsummary[]
array?
The first()
return the first emission for an Observable. So when you have source that emits arrays like INTsummary[]
then it takes the first array it emits (not the first item in the array).
From your description you want to get the first item in the array so there're are two ways:
this.myService.getSummary(this.user.id, this.user.universe)
.map(array => array[0])
... or if you really want to use the first()
operator:
this.myService.getSummary(this.user.id, this.user.universe)
.concatAll() // flatten the array into separate emission
.first()
Right, but the object returned is an array, that what this type states:
Observable<INTsummary[]>
If it returned plain objects, you would have:
Observable<INTsummary>
You can use mergeMap
operator to flatten the array:
var obj = this.myService.getSummary(this.user.id, this.user.universe).first().mergeMap(r => r);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With