How to emit every n-th value?

manidos

I'm using mousemove event to create an observable.

Observable.fromEvent(document, 'mousemove')

I need to emit every 10-th event. What do I do?

martin

I can think of four different ways to do it:

bufferCount()

Observable.range(1, 55)
  .bufferCount(10)
  .map(arr => arr[arr.length - 1])
  .subscribe(val => console.log(val));

windowCount()

Observable.range(1, 55)
  .windowCount(10)
  .switchMap(window => window.takeLast(1))
  .subscribe(val => console.log(val));

debounce()

let source = Observable.range(1, 55).publish();

source
  .debounce(val => debounceNotifier)
  .subscribe(val => console.log(val));

let debounceNotifier = source
  .bufferCount(10)
  .publish();
debounceNotifier.connect();

source.connect();

scan()

Observable.range(1, 55)
  .scan((acc, val) => {
    if (acc.length === 10) {
      acc = [];
    }
    acc.push(val);
    return acc;
  }, [])
  .filter(acc => acc.length === 10)
  .map(acc => acc[acc.length - 1])
  .subscribe(val => console.log(val));

However, when using scan() it'll will discard the last value 55.

See demo for all of them: https://jsbin.com/yagayot/14/edit?js,console

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to subtract every nth value in a column from every (n-x)th value?

Returning value of id column of every n-th row in postgresql

Change value of every n-th index in a list

Python List - set every n-th value None

How to select mixed results at every n-th row?

How to select only every n-th row in table?

How do I do something every n-th attempt?

How to copy every N-th byte(s) of a C array

How to concatenate strings with different separator every n-th element

How to extract every n-th row using array formula

How to make every n'th elements in a list in a list of their own?

Emit elements from a list every n milliseconds

How to get a shared observable to emit a new value from .startWith() every time a shared stream gets a new subscriber?

RxAndroid : How to emit zipped observable every minute?

vuejs how to run emit on every array change

How do we insert \n every n-character or/and after n-th space in a string in R?

Get every n-th element of list starting from maximum value

Hiding xticks labels every n-th label or on value on Pandas plot / make x-axis readable

How to show label on YAxis every N value?

How to add N to every value of a Counter? - python

How to emit v-for value?

How do I make cron run something every "N"th minute, where n % 5 == 1?

How to create new column with values counting up every 9th value with SQL?

For every row how to add a class name on the <th> based on the value on a particular column using Datatables

Group every n-th element of array

Splitting a string at every n-th character

Delete every n-th file Python

Every n-th element of a list

Select every n-th row in Informix?