distinctUntilChanged example RxJS 5

https://stackoverflow.com/questions/44900744/rxjs-observing-partially-a-state-of-an-object

by MichalCafe

HTML

<script src="https://unpkg.com/[email protected]/bundles/Rx.min.js"></script>
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>

<!-- stackoverflow question: https://stackoverflow.com/questions/44900744/rxjs-observing-partially-a-state-of-an-object -->

JavaScript

const object$ = new Rx.BehaviorSubject({
  articleId: 0,
  comments: [],
});

const comments$ = object$
  .map(object => object.comments)
  .distinctUntilChanged((a, b) => _.isEqual(a, b));

comments$.subscribe(v => console.log(v));

object$.next({
  articleId: 1,
  comments: [],
});

object$.next({
  articleId: 1,
  comments: ['lorem', 'ipsum'],
});

object$.next({
  articleId: 2,
  comments: ['lorem', 'ipsum'],
});

object$.next({
  articleId: 2,
  comments: ['lorem', 'ipsum', 'dolor', 'sit', 'amet'],
});