Explain RxJS Aggregate Operators

RxJS provides a set of powerful aggregate operators that allow you to perform various types of aggregations on the values emitted by observables. Here's an overview of some key RxJS aggregate operators, including count, reduce, max, min, scan, and toArray.

1. count

The count operator counts the number of emissions from the source observable that meet a specified condition.

import { of } from 'rxjs';
import { count } from 'rxjs/operators';

const source$ = of(1, 2, 3, 4, 5);

source$.pipe(
  count(value => value % 2 === 0) // Counts even numbers
).subscribe(console.log); // Output: 2

2. reduce

The reduce operator applies an accumulator function over the source observable and returns the accumulated result when the source completes.

import { of } from 'rxjs';
import { reduce } from 'rxjs/operators';

const source$ = of(1, 2, 3, 4, 5);

source$.pipe(
  reduce((acc, value) => acc + value, 0)
).subscribe(console.log); // Output: 15

3. max

The max operator determines the maximum value emitted by the source observable according to a specified criteria.

import { of } from 'rxjs';
import { max } from 'rxjs/operators';

const source$ = of(1, 3, 2, 5, 4);

source$.pipe(
  max()
).subscribe(console.log); // Output: 5

// With custom comparator
source$.pipe(
  max((a, b) => a - b)
).subscribe(console.log); // Output: 5

4. min

The min operator finds the minimum value emitted by the source observable according to a specified criteria.

import { of } from 'rxjs';
import { min } from 'rxjs/operators';

const source$ = of(1, 3, 2, 5, 4);

source$.pipe(
  min()
).subscribe(console.log); // Output: 1

// With custom comparator
source$.pipe(
  min((a, b) => a - b)
).subscribe(console.log); // Output: 1

5. scan

The scan operator applies an accumulator function over the source observable and returns each intermediate result, not just the final accumulated value.

import { of } from 'rxjs';
import { scan } from 'rxjs/operators';

const source$ = of(1, 2, 3, 4, 5);

source$.pipe(
  scan((acc, value) => acc + value, 0)
).subscribe(console.log); // Outputs: 1, 3, 6, 10, 15

6. toArray

The toArray operator collects all source emissions into an array and emits that array when the source completes.

import { of } from 'rxjs';
import { toArray } from 'rxjs/operators';

const source$ = of(1, 2, 3, 4, 5);

source$.pipe(
  toArray()
).subscribe(console.log); // Output: [1, 2, 3, 4, 5]

Example Usage in an Angular Service

To see how you might incorporate these operators into an Angular service, here’s a sample service using some of the aggregate operators:

import { Injectable } from '@angular/core';
import { of } from 'rxjs';
import { count, max, min, reduce, scan, toArray } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class ExampleService {

  countEvenNumbers() {
    return of(1, 2, 3, 4, 5).pipe(
      count(value => value % 2 === 0)
    );
  }

  findMax() {
    return of(1, 3, 2, 5, 4).pipe(
      max()
    );
  }

  findMin() {
    return of(1, 3, 2, 5, 4).pipe(
      min()
    );
  }

  sumValues() {
    return of(1, 2, 3, 4, 5).pipe(
      reduce((acc, value) => acc + value, 0)
    );
  }

  accumulateValues() {
    return of(1, 2, 3, 4, 5).pipe(
      scan((acc, value) => acc + value, 0)
    );
  }

  collectValues() {
    return of(1, 2, 3, 4, 5).pipe(
      toArray()
    );
  }
}

By using these aggregate operators, you can perform complex data transformations and aggregations on the data streams in your Angular applications, making your code more reactive and functional.


Similar Articles