Mitul Panchal
Mitul Panchal

Reputation: 283

Rxjs forkJoin map values from array dynamically

Hi I've an array of Boolean observables and want to perform logical AND operation but as of now I'm passing static values a, b but I don't know how many values would be there inside totalKeys array.

import { forkJoin } from 'rxjs';

...
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
private totalKeys: Observable<Boolean>[] = [];
// some logic
  
return forkJoin(this.totalKeys).pipe(
    map(([a, b]) => a && b)
  );
};

How can I dynamically add elements to map array and use logical AND on those instead of passing static params to map operator.

Upvotes: 0

Views: 468

Answers (2)

Eddy Lin
Eddy Lin

Reputation: 683

RxJS

enter image description here

like this

merge(...totalKeys).pipe(every(identity))

Upvotes: 1

Matthieu Riegler
Matthieu Riegler

Reputation: 55028

Use every :

which will look like following for you.

map(items => items.every(Boolean))

The way it works is following : every item is converted to a boolean and every will check that every item is true. this is semanticly equivalent to having && on every item.

Upvotes: 2

Related Questions