Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS keep old and new value on emission

Tags:

rxjs

pairwise

I have been trying to get the old and new value in every emission. I have seen the option of using pairwise or bufferCount but they don't allow to keep the first value.

The goal would to go from:

---1---2---3---4---5---

To:

---null,1---1,2---2,3---3,4---4,5---

Any ideas?

like image 586
Roman Oxman Avatar asked Oct 18 '25 11:10

Roman Oxman


1 Answers

You can use startWith(null) to initialize the operator (whichever you use) and then it'll emit on every value:

// or bufferCount(2, 1)
source.startWith(null).pairwise().subscribe(...)
like image 131
martin Avatar answered Oct 22 '25 05:10

martin