Search code examples
angularrxjsrxjs-pipeable-operators

How to change multi filter operators with a brief solution, in Rx Js observables?


I want to write this code with a relatively short code, can you advise some solution? Thanks!

fromEvent(document, 'click').pipe(
filter(e => e.target !== this.one.nativeElement),
filter(e => e.target !== this.two.nativeElement),
filter(e => e.target !== this.three.nativeElement),
map(() => console.log('target'))
.subscribe();

Solution

  • fromEvent(document, 'click').pipe(
      filter(e => ![
        this.one.nativeElement, 
        this.two.nativeElement, 
        this.three.nativeElement
      ].some(elem => e.target === elem)),
      map(() => console.log('target'))
    ).subscribe();