Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript RxJS-如何映射/切换可观察的对象,并为每个发射值添加平均值计算?_Javascript_Rxjs_Reactivex_Rxjs6 - Fatal编程技术网

Javascript RxJS-如何映射/切换可观察的对象,并为每个发射值添加平均值计算?

Javascript RxJS-如何映射/切换可观察的对象,并为每个发射值添加平均值计算?,javascript,rxjs,reactivex,rxjs6,Javascript,Rxjs,Reactivex,Rxjs6,我是RxJS的新手,我想做以下工作: 想象一下,我正在写一个方法来跟踪温度随时间的变化。此方法将观察一个对象,该对象将发出如下值:[12,49,-2,26,5,…] 我怎样才能将其转化为另一个可观测值,随着时间的推移,它会增加每个值的平均值 [ { temperature: 12, mean: 12 }, { temperature: 49, mean: 30.5 }, { temperature: -2, mean: 19.6

我是RxJS的新手,我想做以下工作:

想象一下,我正在写一个方法来跟踪温度随时间的变化。此方法将观察一个对象,该对象将发出如下值:
[12,49,-2,26,5,…]

我怎样才能将其转化为另一个可观测值,随着时间的推移,它会增加每个值的平均值

[
  {
    temperature: 12,
    mean: 12
  },
  {
    temperature: 49,
    mean: 30.5
  },
  {
    temperature: -2,
    mean: 19.67
  },
  {
    temperature: 26,
    mean: 21.25
  },
  {
    temperature: 5,
    mean: 18
  },
  ...
]
我正在努力解决的困难是,平均值计算应该使用所有以前的值来完成


有办法做到这一点吗?实际上,我需要添加更多数据并计算其他值,但这就是我需要做的要点。

像使用阵列上的reduce一样使用扫描。用{num:0,total:0,mean:0}传入一个起始累加器,每次迭代递增num,将当前温度添加到总温度中并计算平均值。将观测值视为随时间变化的阵列有时有助于可视化它们

const{from,timer,zip}=rxjs;
const{scan}=rxjs.operators;
常数temps=[12,49,-2,26,5];
//让我们用数组来实现它
console.log(
降温(
(蓄能器,温度)=>({
num:acculator.num+1,
临时工:临时工,
总计:蓄能器。总计+温度,
平均值:(acculator.total+temp)/(acculator.num+1)
}),
{num:0,temp:0,total:0,mean:0}
)
);
const temps$=from(temps);
常量计时器$=计时器(0,1500);
var tempsOverTime$=zip(temps$,timer$,(temp,)=>temp);
//现在,让我们对一个随时间变化的可观测对象做同样的事情。
临时性$
.烟斗(
扫描(
(蓄能器,温度)=>({
num:acculator.num+1,
临时工:临时工,
总计:蓄能器。总计+温度,
平均值:(acculator.total+temp)/(acculator.num+1)
}),
{num:0,temp:0,total:0,mean:0}
)
)
.订阅(a=>{
log({temp:a.temp,mean:a.mean});
});

像在阵列上使用reduce一样使用扫描。用{num:0,total:0,mean:0}传入一个起始累加器,每次迭代递增num,将当前温度添加到总温度中并计算平均值。将观测值视为随时间变化的阵列有时有助于可视化它们

const{from,timer,zip}=rxjs;
const{scan}=rxjs.operators;
常数temps=[12,49,-2,26,5];
//让我们用数组来实现它
console.log(
降温(
(蓄能器,温度)=>({
num:acculator.num+1,
临时工:临时工,
总计:蓄能器。总计+温度,
平均值:(acculator.total+temp)/(acculator.num+1)
}),
{num:0,temp:0,total:0,mean:0}
)
);
const temps$=from(temps);
常量计时器$=计时器(0,1500);
var tempsOverTime$=zip(temps$,timer$,(temp,)=>temp);
//现在,让我们对一个随时间变化的可观测对象做同样的事情。
临时性$
.烟斗(
扫描(
(蓄能器,温度)=>({
num:acculator.num+1,
临时工:临时工,
总计:蓄能器。总计+温度,
平均值:(acculator.total+temp)/(acculator.num+1)
}),
{num:0,temp:0,total:0,mean:0}
)
)
.订阅(a=>{
log({temp:a.temp,mean:a.mean});
});