Skip to content

Latest commit

 

History

History
53 lines (38 loc) · 2.08 KB

distinctuntilchanged.md

File metadata and controls

53 lines (38 loc) · 2.08 KB

distinctUntilChanged

연산자(operator) 정의: distinctUntilChanged(compare: function): Observable

Only emit when the current value is different than the last.


💡 distinctUntilChanged uses === comparison by default, object references must match!


Examples

Example 1: distinctUntilChanged with basic values

( jsBin | jsFiddle )

//only output distinct values, based on the last emitted value
const myArrayWithDuplicatesInARow = Rx.Observable
  .from([1,1,2,2,3,1,2,3]);
  
const distinctSub = myArrayWithDuplicatesInARow
	.distinctUntilChanged()
  	//output: 1,2,3,1,2,3
	.subscribe(val => console.log('DISTINCT SUB:', val));
  
const nonDistinctSub = myArrayWithDuplicatesInARow
	//output: 1,1,2,2,3,1,2,3
	.subscribe(val => console.log('NON DISTINCT SUB:', val));
Example 2: distinctUntilChanged with objects

( jsBin | jsFiddle )

const sampleObject = {name: 'Test'};
//Objects must be same reference
const myArrayWithDuplicateObjects = Rx.Observable.from([sampleObject, sampleObject, sampleObject]);
//only out distinct objects, based on last emitted value
const nonDistinctObjects = myArrayWithDuplicateObjects
  .distinctUntilChanged()
  //output: 'DISTINCT OBJECTS: {name: 'Test'}
  .subscribe(val => console.log('DISTINCT OBJECTS:', val));

Additional Resources


📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/operator/distinctUntilChanged.ts