# distinctUntilChanged

#### signature: `distinctUntilChanged(compare: function): Observable`

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

:bulb: distinctUntilChanged uses `===` comparison by default, object references must match!

:bulb: If you want to compare based on an object property, you can use [`distinctUntilKeyChanged`](/learn-rxjs-korean/learn-rxjs/recipes-1/filtering/distinctuntilkeychanged.md) instead!

### Examples

**Example 1: distinctUntilChanged with basic values**

( [StackBlitz](https://stackblitz.com/edit/typescript-bsb8mw?file=index.ts\&devtoolsheight=100) )

```javascript
// RxJS v6+
import { from } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';

// only output distinct values, based on the last emitted value
const source$ = from([1, 1, 2, 2, 3, 3]);

source$
  .pipe(distinctUntilChanged())
  // output: 1,2,3
  .subscribe(console.log);
```

**Example 2: distinctUntilChanged with objects**

( [StackBlitz](https://stackblitz.com/edit/typescript-moe7mh?file=index.ts\&devtoolsheight=100) )

```javascript
// RxJS v6+
import { from } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';

const sampleObject = { name: 'Test' };

//Objects must be same reference
const source$ = from([sampleObject, sampleObject, sampleObject]);

// only emit distinct objects, based on last emitted value
source$
  .pipe(distinctUntilChanged())
  // output: {name: 'Test'}
  .subscribe(console.log);
```

**Example 3: Using custom comparer function**

( [StackBlitz](https://stackblitz.com/edit/typescript-hzta27?file=index.ts\&devtoolsheight=100) )

```javascript
// RxJS v6+
import { from } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';

// only output distinct values, based on the last emitted value
const source$ = from([
  { name: 'Brian' },
  { name: 'Joe' },
  { name: 'Joe' },
  { name: 'Sue' }
]);

source$
  // custom compare for name
  .pipe(distinctUntilChanged((prev, curr) => prev.name === curr.name))
  // output: { name: 'Brian }, { name: 'Joe' }, { name: 'Sue' }
  .subscribe(console.log);
```

### Related Recipes

* [Lockscreen](/learn-rxjs-korean/learn-rxjs/recipes/lockscreen.md)
* \[Save Indicator]\('../../recipes/save-indicator.md)
* [Type Ahead](/learn-rxjs-korean/learn-rxjs/recipes/type-ahead.md)

### Additional Resources

* [distinctUntilChanged](https://rxjs.dev/api/operators/distinctUntilChanged)

  :newspaper: - Official docs
* [Filtering operator: distinct and distinctUntilChanged](https://egghead.io/lessons/rxjs-filtering-operators-distinct-and-distinctuntilchanged?course=rxjs-beyond-the-basics-operators-in-depth)

  :video\_camera: :dollar: - André Staltz

> :file\_folder: Source Code: <https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/distinctUntilChanged.ts>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://junwoo45.gitbook.io/learn-rxjs-korean/learn-rxjs/recipes-1/filtering/distinctuntilchanged.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
