javascript - 如何使用reduce、pipe和combinelatest求和?

标签 javascript rxjs rxjs5 rxjs-pipeable-operators rxjs-observables

值是输入字段上的可观察值列表

    var example = combineLatest(values);

如何查找文本字段中的值总和。

    example.subscribe(val => {
        console.log('Sum:', val);
    });

让订阅者通过它给我表单的输出

总和:(2) ["1", "2"]

通过combineLatest管道传递给我NaN

     .pipe(reduce((acc, one) => {
         var a =Number(acc) + Number(one);
         console.log(a);
         return a;
     }, 0));

最佳答案

你不必减少 Observable,你必须减少它发出的数组。

var example = combineLatest(values).pipe(
  map(array => array.reduce((pv, cv) => pv + Number(cv), 0))
);

关于javascript - 如何使用reduce、pipe和combinelatest求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60569209/

相关文章:

Angular 4 - 过滤时将 HTTP 请求限制为每 200 毫秒一次

rxjs 合并和错误处理

angular - RxJS 5 - 最后,最终确定,完成,没有任何效果

javascript - 使用 jquery 切换显示/隐藏

javascript - 在typescript中使用rxjs时出现错误

javascript - 对页面加载后提交按钮时出现的表格进行排序

javascript - RxJS:具有中止功能的生产者-消费者

javascript - 如何在 Angular 2 中使用 RXJS 观察自定义事件?

javascript - 在 Javascript 测试中断言对象相等 (chai)

javascript - 媒体流到 JavaScript 中的 RTMP