In my Angular13 component I have an observable sending out a stream of data.
I want to do some data manipulation of this data outside of the Observable.
myObservable$: Observable<ReadonlyArray<MyObject>> = this.store.select( get it from global ngrx store )
globalArray: Array<MyObject> = [];
ngOnInit(): void {
this.myObservable$.subscribe(x => {
let results = x.reduce((accum: any, item: MyObject) => {
// does some data manipulation reduce stuff
}, {});
console.log(`results is ...`, results);
this.globalArray = results; // Does not work, globalArray remains blank.
this.globalArray.push(results) // Does not work, I get a global array with a blank first entry and then 2 other entries containing ALL the data.
});
// I want to do stuff to globalArray here, outside of the Observable
// modify it's data quite a bit more
// have other components use it as a data source.
}
Also if I try to just make the global array equal the observable I get another error:
// Error: Type 'Subscription' is missing the following properties from type MyObject: : length, pop, push, concat, and 28 more
this.globalArray = this.myObservable$.subscribe(x => {
let results = x.reduce((accum: any, item: MyObject) => {
// does some data manipulation reduce stuff
}, {});
return results;
});
Any help appreciated! I feel that I must be working with Observables the wrong way? This is probably a masterclass of how not to use observables.