ArticleZip > How To Use Rxjs Distinctuntilchanged

How To Use Rxjs Distinctuntilchanged

RxJS is a powerful library that allows developers to work with reactive programming concepts. One useful operator in RxJS is `distinctUntilChanged`. This operator filters out consecutive duplicate values emitted by an observable stream. It comes in handy when you want to ensure that only distinct consecutive values are processed further in your code.

To use `distinctUntilChanged` in your code, you first need to import it from the RxJS library. Here is an example of how you can import `distinctUntilChanged`:

Javascript

import { distinctUntilChanged } from 'rxjs/operators';

Next, you can apply `distinctUntilChanged` to an observable stream by using the `pipe` method. The `pipe` method allows you to chain multiple operators together. Here is an example of how you can use `distinctUntilChanged` to filter out consecutive duplicate values from an observable stream:

Javascript

import { from } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';

const source = from([1, 1, 2, 2, 3, 3, 3, 4]);
const distinctSource = source.pipe(distinctUntilChanged());

distinctSource.subscribe(console.log);

In this example, we first create an observable `source` that emits a sequence of numbers. We then use `distinctUntilChanged` to create a new observable `distinctSource` that filters out consecutive duplicate values. Finally, we subscribe to `distinctSource` and log the emitted values to the console.

It's important to note that `distinctUntilChanged` compares the current value emitted by the observable with the previous value. If the current value is the same as the previous value, the operator will not emit the value. This is why only distinct consecutive values are passed down the observable chain.

You can also provide a custom compare function to `distinctUntilChanged` if you need more control over how values are compared. The compare function takes two arguments - the previous value and the current value - and should return a boolean value indicating whether the values are considered distinct.

Javascript

import { from } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';

const source = from([
  { id: 1, name: 'Alice' },
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' },
]);
const distinctSource = source.pipe(
  distinctUntilChanged((prev, curr) => prev.id === curr.id)
);

distinctSource.subscribe((val) => console.log(val.name));

In this example, we compare objects based on their `id` property to determine if they are distinct. This allows us to filter out objects with the same `id` value but different `name` values.

By using `distinctUntilChanged` in your RxJS code, you can ensure that only unique consecutive values are processed, making your code more efficient and concise. Try incorporating this operator into your reactive programming workflows to manage data streams effectively.

×