single

signature: single(a: Function): Observable

Emit single item that passes expression.

single - 图1

Examples

Example 1: Emit first number passing predicate

( jsBin |
jsFiddle )

  1. import { from } from 'rxjs/observable/from';
  2. import { single } 'rxjs/operators';
  3. //emit (1,2,3,4,5)
  4. const source = from([1, 2, 3, 4, 5]);
  5. //emit one item that matches predicate
  6. const example = source.pipe(single(val => val === 4));
  7. //output: 4
  8. const subscribe = example.subscribe(val => console.log(val));

Additional Resources


:file_folder: Source Code:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/single.ts