RxJS distinctUntilChanged - object comparsion

后端 未结 7 784
青春惊慌失措
青春惊慌失措 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:28

    I had the same problem, and fixed it with using JSON.stringify to compare the objects:

    .distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b))

    Dirty but working code.

    0 讨论(0)
  • 2021-01-03 21:30

    Mehdi's solution although fast but wouldn't work if the order is not maintained. Using one of deep-equal or fast-deep-equal libraries:

    .distinctUntilChanged((a, b) => deepEqual(a, b))
    
    0 讨论(0)
  • You can also wrap the original distinctUntilChanged function.

    function distinctUntilChangedObj<T>() {
      return distinctUntilChanged<T>((a, b) => JSON.stringify(a) === JSON.stringify(b));
    }
    

    This lets you use it just like the original.

    $myObservable.pipe(
      distinctUntilChangedObj()
    )
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-03 21:43

    If you change the value mutably, none of the other answers work. If you need to work with mutable data structures, you can use distinctUntilChangedImmutable, which deep copies the previous value and if no comparison function is passed in, will assert that the previous and the current values deep equals each other (not === assertion).

    0 讨论(0)
  • 2021-01-03 21:46

    I finally figure out where problem is. Problem was in version of RxJS, in V4 and earlier is different parameters order than V5.

    RxJS 4:

    distinctUntilChanged = function (keyFn, comparer)

    RxJS 5:

    distinctUntilChanged = function (comparer, keyFn)

    In every docs today, you can find V4 parameters order, beware of that!

    0 讨论(0)
提交回复
热议问题