Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use distinctUntilChanged on objects

How can I have distinctUntilChanged work with objects like this

myObs = Observable.from([{foo: 'bar'}, {foo: 'bar'}]);

myObs.distinctUntilChanged()
  .subscribe(value => {
  // I only want to receive {foo: 'bar'} once
});
like image 882
Lev Avatar asked Nov 06 '17 15:11

Lev


People also ask

What is from in RXJS?

from converts various other objects and data types into Observables. It also converts a Promise, an array-like, or an iterable object into an Observable that emits the items in that promise, array, or iterable. A String, in this context, is treated as an array of characters.

What is of operator in RXJS?

The of Operator is a creation Operator. Creation Operators are functions that create an Observable stream from a source. The of Operator will create an Observable that emits a variable amount of values in sequence, followed by a Completion notification.


1 Answers

You need to pass a function inside the distinctUntilChanged that returns a boolean to make sure that the objects are the same.

Example

    myObs. distinctUntilChanged((a, b) => a.foo === b.foo)
like image 166
Fabio Carpinato Avatar answered Nov 15 '22 08:11

Fabio Carpinato