rxjs-scan

5 篇文章 0 订阅

arr.reduce(callback)

参考:https://www.cnblogs.com/leejersey/p/5466091.html

callback:执行数组中每个值的函数,包含四个参数

// 语法:
arr.reduce((previousValue, currentValue, index, array) => {
    return previousValue + currentValue;
}, initialValue)

参数:
previousValue:上一次调用回调返回的值,或者是提供的初始值(initialValue)。
currentValue 数组中当前被处理的元素。
index当前元素在数组中的索引。
array调用 reduce 的数组。
initialValue作为第一次调用 callback 的第一个参数。

注:initialValue可以省略;
如果省略的话,那么previousValue取arr[0],currentValue取arr[1];执行arr.length-1次。
如果没有省略的话,那么previousValue取initialValue,currentValue取arr[0];执行arr.length次。

rxjs-scan:

example1:累加

const source = of(1, 2, 3);

// 基础的 scan 示例,从0开始,随着时间的推移计算总数
const example = source.pipe(scan((prev, curr) => {
  console.log(prev, curr);
  return prev + curr;
}, 0));

// 输出累加值
// 输出: 1,3,6

const subscribe = example.subscribe(val => {
  console.log(val)
});

or:example2:数组

const scanObs = interval(1000)
  .pipe(
    take(6),
    scan((prev:any, curr) => [...prev, curr], []),
  )
  .subscribe(console.log);

结果:

// [0]
// [0, 1]
// ...
// [0, 1, 2, 3, 4, 5]

注:
1.参数的顺序
2.subscribe直接传了个fn,也是可以的。
3.prev一定要带有参数类型,要不然报错的。eg:https://blog.csdn.net/loongsking/article/details/78271753

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值