0

const observable = Rx.Observable.create( observer => {
    observer.next( 'hello' )
    observer.next( 'world' )
})

observable.subscribe(val => console.log(val))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/rxjs/4.1.0/rx.min.js"></script>

In this above script, when we subscribe, we get both values (hello and world). How do we get individual values separately. Example: We want to get only the 2nd value.

1 Answer 1

2

You can use the skip()-operator.

import { skip } from 'rxjs/operators';

observable.pipe(skip(1)).subscribe(val => console.log(val))

If you don't just want to skip values but filter them, filter() is your rxjs-function of choice. Because here, equal to the filter-method of an array, you can define a so called predicate which is the condition by which an object passes or gets dropped.

Here is a small example. It only lets strings pass that contain an uppercase A.

import {Subject} from 'rxjs';
import {filter} from 'rxjs/operators';

private observable: Subject<string> = new Subject<string>();

ngOnInit(): void {
    this.observable.pipe(filter(value => value.includes('A'))).subscribe(val => console.log(val));

    this.observable.next('AA');
    this.observable.next('BA');
    this.observable.next('BB');
    this.observable.next('CC');
    this.observable.next('DD');
    this.observable.next('DA');
    this.observable.next('AD');
}
Sign up to request clarification or add additional context in comments.

3 Comments

PS: If this solved your question, please, be so kind and mark this answer as solution.
Thanks for your option @lynx 242. Sure I will do. Is there any other option available kind of key value? instead of skipping with number.
So, I extended my solution. Hope this helps. Once again, please, don't forget to mark the solution as solution. Thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.