RxJS 5.5.2为每个订阅创建于扫描操作符的新种子对象
我有下面的代码谁分裂的数字阵列为对象,具有2个属性“小”的数字越小则4和'大'其余。
const o = from([1, 2, 3, 4, 5, 6]).pipe(
scan<number, {}>((a, b) => {
if (b < 4) {
a['small'].push(b);
} else {
a['big'].push(b);
}
return a;
}, {
'small': [],
'big': []
})
);
console.log('subscription 1');
o.subscribe(x => console.log(JSON.stringify(x)));
console.log('subscription 2');
o.subscribe(x => console.log(JSON.stringify(x)));
认购1控制台打印后:
{"small":[1,2,3],"big":[4,5,6]} // this is ok
订购2台打印后:
{"small":[1,2,3,1,2,3],"big":[4,5,6,4,5,6]} // this is not ok
有没有开始一个新的种子对象每次有人赞同呢?
看来OP正在使用Typescript,所以你可以简化使用ES7赋值符号'{... a,small:[... a.small,b]}' – paulpdaniels
@paulpdaniels好点 –