zoukankan      html  css  js  c++  java
  • RxjsScan

     1 scan( (previousValue,currentValue)=>previousValue+currentValue),seed?) 

    函数功能:

    提供一个数组(Observable),对数组的元素累计求和

    参数解析:

     previousValue: 上一次调用回调返回的值,或者是提供的初始值

     currentValue: 数组中当前被处理的元素

     seed: 用以提供初始值,如果 seed 未被定义,则数组的第一个元素被当作初始值,此时第一次并不会被计算,而是将元素的第一个值作为第一个返回值,将返回值作为参数,与第二个数组元素相加,计算结果作为第二个返回值,依次计算。

    返回结果:一系列可观察对象

    示例:

    const number$ = of(1, 2, 3)
    number$.pipe( scan((total, n) => total + n)).subscribe(x => {
            console.log(x);
        })

    上面代码中,提供了 previousValue 和 currentValue,没有提供 seed,所以第一次执行时候,将 1 作为初始状态返回,然后执行 1+2,返回 3, 再执行 3+3,返回 6,返回结果是 1,3,6

    const number$ = of(1, 2, 3)
    number$.pipe( scan((total, n) => total + n)).subscribe(x => {
            console.log(x);
        })

    上面代码中,提供了 previousValue 、 currentValue 和 seed,seed作为起始状态,所以第一次执行时候,执行1+4,返回5,然后执行 5+2,返回 7, 再执行 7+3,返回 10,返回结果是 5,7,10

  • 相关阅读:
    linux 清理cache中的内存
    科学计算和可视化
    Python生成随机数的方法
    matplotlib绘图的基本操作
    python中的数组和列表
    Python人工智能学习笔记
    利用numpy+matplotlib绘图的基本操作教程
    split函数用法
    玩转PIL库
    广师大学习笔记之文本统计(jieba库好玩的词云)
  • 原文地址:https://www.cnblogs.com/wyjblog/p/15556555.html
Copyright © 2011-2022 走看看