Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I do it with just one Behaviorsubject?

I have this code:

private _fileData$: BehaviorSubject<string> = new BehaviorSubject<string>('');
private _storageData$: BehaviorSubject<Array<string>> = new BehaviorSubject<Array<string>>([]);

    private constructor() {
        this._fileData$.subscribe({ next: (fileData: string) => this._storageData$.next(fileData.toString().split('\r\n')) });

So i have some string data in _fileData$ and I want to split it by lines so I get Array of strings, so im creating _storageData$ to put them here.
It works like I want to, but how can I do it using only one BehaviorSubject?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

private _storageData$: Observable<Array<string>>;

this._storageData$ = this._fileData$
    .pipe(map((fileData) => {
        return fileData.toString().split('\r\n');
    }));

Then you can bind to it in your view:

<span>{{ _storageData$ | async }}</span>

map is an rxjs operator

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading