How Does Rxjs Reduce In Case There Is No Matching Accumulator?
I was going through an article on Reactive Programming in JavaScript and not sure how the following example listed in there results in output 27 import {Observable} from 'rxjs-es';
Solution 1:
I have translated it to RxJs 6 and it doesn't output 27
const { interval } = rxjs;
const { map, filter, reduce, take } = rxjs.operators;
let output = interval(500).pipe(
map(i => [1,2,3,4,5,6][i]), // very strange way of adding one to the intervaltake(6) // had to add a take so observable would complete else reduce would never emit
);
let result = output.pipe(
map(num1 => num1), // Does nothingfilter(num1 => num1 > 4), // filters out those less than 5reduce((num1, num2) => num1 + num2) // add the leftovers 5 and 6
);
result.subscribe(number =>console.log(number)); // 11 is the output
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.6.2/rxjs.umd.min.js"></script>
Post a Comment for "How Does Rxjs Reduce In Case There Is No Matching Accumulator?"