0

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.

1
  • 3
    You can use the reduce() operator to perform a logical AND operation on the elements of an array return forkJoin(this.totalKeys).pipe( map(values => values.reduce((acc, val) => acc && val)) ); Commented Dec 5, 2022 at 7:51

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Would be great if you can elaborate more on this, how would I perform AND operation on each value.
@MatthieuRiegler shouldn't it be map(items => items.every(Boolean))?
@akotech yes, you're right
1

RxJS

enter image description here

like this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.