Skip to content

Instantly share code, notes, and snippets.

@NyaGarcia
Last active April 4, 2021 10:45
Show Gist options
  • Save NyaGarcia/f03d3e6217a974c41d9af5fbc68809da to your computer and use it in GitHub Desktop.
Save NyaGarcia/f03d3e6217a974c41d9af5fbc68809da to your computer and use it in GitHub Desktop.
Duplicated logic in Observables
import { from } from "rxjs";
import { filter, reduce } from "rxjs/operators";
const number$ = from([null, 2, 1, undefined, 5, false, 6, 7]);
// Adding numbers
number$
.pipe(
filter<number>(Boolean),
reduce((acc, curr) => acc + curr)
)
.subscribe(n => console.log(`Total: ${n}`));
// Emit even numbers
number$
.pipe(
filter<number>(Boolean),
filter(n => n % 2 === 0)
)
.subscribe(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment