RxJS distinctUntilChanged - object comparsion

后端 未结 7 822
青春惊慌失措
青春惊慌失措 2021-01-03 20:57

I have stream of objects and I need to compare if current object is not same as previous and in this case emit new value. I found distinctUntilChanged operator should do exa

7条回答
  •  無奈伤痛
    2021-01-03 21:43

    From RxJS v6+ there is distinctUntilKeyChanged

    https://www.learnrxjs.io/operators/filtering/distinctuntilkeychanged.html

    const source$ = from([
      { name: 'Brian' },
      { name: 'Joe' },
      { name: 'Joe' },
      { name: 'Sue' }
    ]);
    
    source$
      // custom compare based on name property
      .pipe(distinctUntilKeyChanged('name'))
      // output: { name: 'Brian }, { name: 'Joe' }, { name: 'Sue' }
      .subscribe(console.log);
    

提交回复
热议问题