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好點 –