Javascript 如何使用 RxJs distinctUntilChanged?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36272036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:51:56  来源:igfitidea点击:

How to use RxJs distinctUntilChanged?

javascriptrxjsrxjs5

提问by Thijs Koerselman

I'm getting started with RxJs (using the v5 beta), but somehow I can't figure out how to work with distinctUntilChanged. The output from the code below if I run it in babel-node is

我开始使用 RxJs(使用 v5 测试版),但不知何故我无法弄清楚如何使用distinctUntilChanged. 如果我在 babel-node 中运行它,下面代码的输出是

[ 'a', 1 ]
{ key: 'a', state: 1 }
Next:  { value: 42 }
Completed

That is not what I would expect. Why is only one entry passing distinctUntilChanged? I would expect the output to be

这不是我所期望的。为什么只有一个条目通过distinctUntilChanged?我希望输出是

[ 'a', 1 ]
[ 'a', 0 ]
[ 'a', 1 ]
{ key: 'a', state: 1 }
{ key: 'a', state: 2 }
{ key: 'a', state: 0 }
{ key: 'a', state: 1 }
Next:  { value: 42 }
Next:  { value: 24 }
Completed

Here's the code

这是代码

import {Observable} from 'rxjs'

Observable.of(['a', 1], ['a', 1], ['a', 0], ['a', 1])
  .distinctUntilChanged(x => x[1])
  .subscribe(x => console.log(x))

Observable.of({key: 'a', state: 1}, {key: 'a', state: 2}, {key: 'a', state: 0}, {key: 'a', state: 1})
  .distinctUntilChanged(x => x.state)
  .subscribe(x => console.log(x))

Observable.of({value: 42}, {value: 42}, {value: 24}, {value: 24})
  .distinctUntilChanged(x => x.value)
  .subscribe(
    function (x) {
      console.log('Next: ', x)
    },
    function (err) {
      console.log('Error: ' + err)
    },
    function () {
      console.log('Completed')
    }
  )

The linksin the v5 docs for these functions appear to be dead

这些函数的 v5 文档中的链接似乎已失效

------ edit -----

- - - 编辑 - - -

Some additional debugging:

一些额外的调试:

Observable.of(['a', 1], ['a', 1], ['a', 0], ['a', 1])
  .do(x => console.log('before', x))
  .distinctUntilChanged(x => x[1])
  .do(x => console.log('after', x))
  .subscribe(x => console.log(x))

output:

输出:

before [ 'a', 1 ]
after [ 'a', 1 ]
[ 'a', 1 ]
before [ 'a', 1 ]
before [ 'a', 0 ]
before [ 'a', 1 ]

回答by Thijs Koerselman

I got an answer here. Basically the function signature changed from (key selector, comparator) to (comparator, key selector).

我在这里得到了答案。基本上函数签名从(键选择器,比较器)更改为(比较器,键选择器)。

This is how the example is done in v5:

这是在 v5 中完成示例的方式:

Observable.of(['a', 1], ['a', 1], ['a', 0], ['a', 1])
  .distinctUntilChanged(null, x => x[1])
  .subscribe(x => console.log(x))

回答by user3743222

Here is a samplewith your code is Rxjs V4. You will see that it works correctly.

这是一个示例,您的代码是 Rxjs V4。你会看到它工作正常。

Observable.of(['a', 1], ['a', 1], ['a', 0], ['a', 1])
  .distinctUntilChanged(x => x[1])
  .subscribe(x => console.log(x))
...

So it seems to be something with the new beta version. Here are the specs for distinctUntilChanged. The operator itself seems to be working as in version 4.

所以它似乎与新的测试版有关。以下是distinctUntilChanged的规格。操作员本身似乎像在版本 4 中一样工作。

To test things out, I recommend you trace the output of each function by inserting a .do(function(x){console.log(x)})in between operators. I can only think of the ofoperator maybe passing on only the last element of the array.

为了进行测试,我建议您通过.do(function(x){console.log(x)})在运算符之间插入 a来跟踪每个函数的输出。我只能想到of运算符可能只传递数组的最后一个元素。