gpt4 book ai didi

javascript - RxJS groupBy和combineAll运算符似乎省略了输出

转载 作者:行者123 更新时间:2023-11-27 22:51:12 26 4
gpt4 key购买 nike

使用 .groupBy 和 .concatAll 组合对输出进行分组时,不会生成一些预期的输出。

示例代码:

var Rx = require('rx');

var source = Rx.Observable.from(['a1', 'a2', 'b1', 'b2', 'a3', 'a4', 'b3', 'b4'])
.groupBy(function (item) { return item.substr(0, 1); })
.concatAll();

var subscription = source.subscribe(
function (x) {
console.log('Next: %s', x);
},
function (err) {
console.log('Error: %s', err);
},
function () {
console.log('Completed');
});

实际输出:

$ node index.js
Next: a1
Next: a2
Next: a3
Next: a4
Completed

预期输出:

$ node index.js
Next: a1
Next: a2
Next: a3
Next: a4
Next: b1
Next: b2
Next: b3
Next: b4
Completed

我是否误解了这些运算符的工作原理?或者这是一个 RxJS 错误? (暂时已在 https://github.com/Reactive-Extensions/RxJS/issues/1264 处提交为错误。)

最佳答案

想通了。这是hot vs cold observables的问题。按如下方式更改代码使其按预期工作:

var source = Rx.Observable.from(['a1', 'a2', 'b1', 'b2', 'a3', 'a4', 'b3', 'b4'])
.groupBy(function (item) { return item.substr(0, 1); })
.map(function (obs) { // <<<<< ADD THIS .map clause to fix
var result = obs.replay();
result.connect();
return result;
})
.concatAll();

关于javascript - RxJS groupBy和combineAll运算符似乎省略了输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38043728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com