I need to get to Observable<any[]>
. I currently have an observable that looks like this:
const eventsWatcher = new Observable(observer => {
try {
this.socket.on('calendar-events', data => {
observer.next(data);
});
}
catch (err) {
observer.error(err);
}
})
Basically I need to take the values from this Observable and create a new one that is an array of the values emitted by the existing observable.
I tried
const arr = [];
const eventsWatcher = new Observable(observer => {
try {
this.socket.on('calendar-events', data => {
observer.next(data);
});
}
catch (err) {
observer.error(err);
}
}).subscribe(data => {
arr.push(data);
});
return Observable.from(arr);
But that returns an ArrayObservable
instead of an Observable<any[]>
.
Sorry if I am not describing this well, but rxjs is really hard.
Use the scan operator:
const yourNewObservable = eventsWatcher
.scan((acc, newItem) => acc.concat(newItem), []);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.