I am faced with the challenge of transforming and aggregating elements in an observable array that is continuously open (network-based) and may never complete.
Currently, my code involves the following:
const numbers = [1,2,3];
const numbers$ = new Rx.Subject();
const output = numbers$
.flatMap(n => n)
.map(n => n*n)
.scan((acc, x) => acc.concat([x]), [])
.subscribe(n => { console.log(n); });
numbers$.next(numbers);
setTimeout(() => {
numbers$.next([5,6,7])
}, 1000);
At present, multiple arrays are being emitted and the final emitted value is currently [1, 4, 9, 25, 36, 49]
. However, my goal is to only square values that are part of the same input array.
In other words, I am looking for the output observable to emit two arrays: [1,4,9]
and [25, 36, 49]
.
Does anyone have a solution to achieve this?