skip

signature: skip(the: Number): Observable

Skip the provided number of emitted values.

Why use skip?

Skip allows you to ignore the first x emissions from the source. Generally
skip is used when you have an observable that always emits certain values on
subscription that you wish to ignore. Perhaps those first few aren’t needed or
you are subscribing to a Replay or BehaviorSubject and do not need to act on
the initial values. Reach for skip if you are only concerned about later
emissions.

You could mimic skip by using filter with indexes. Ex.
.filter((val, index) => index > 1)

skip - 图1

Examples

Example 1: Skipping values before emission

( jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { skip } 'rxjs/operators';
  3. //emit every 1s
  4. const source = interval(1000);
  5. //skip the first 5 emitted values
  6. const example = source.pipe(skip(5));
  7. //output: 5...6...7...8........
  8. const subscribe = example.subscribe(val => console.log(val));

Example 2: Short hand for a specific filter use case

( jsBin |
jsFiddle )

  1. import { from } from 'rxjs/observable/from';
  2. import { skip, filter } 'rxjs/operators';
  3. const numArrayObs = from([1,2,3,4,5,6,7,8,9,10]);
  4. // 3,4,5...
  5. const skipObs = numArrayObs.pipe(skip(2))
  6. .subscribe(console.log);
  7. // 3,4,5...
  8. const filterObs = numArrayObs.pipe(
  9. filter((val, index) => index > 1)
  10. )
  11. .subscribe(console.log);
  12. //Same output!

Additional Resources


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