About the usage of double exclamation marks in Rxjs filter operator in Angular applications

Look at the following code that appears in Angular Component:

protected userCostCenters$: Observable<CostCenter[]> =
    this.userCostCenterService
      .getActiveCostCenters()
      .pipe(filter((costCenters) => !!costCenters));

This Angular component code involves Observable and RxJS operators, especially filteroperators, and the use of double exclamation points (!!). I will explain step by step what this code means and provide examples to understand it better.

1. Observable and RxJS

In Angular applications, Observable is part of the RxJS library and is used to handle asynchronous data streams. Observable represents a series of values ​​that can change over time. Typically, we apply various operators to Observables to filter, transform, and manipulate these values.

2. Observable<CostCenter[]>

Here, userCostCenters$it is a variable of type Observable, which represents a CostCenter array ( CostCenter[]). This means it will emit a CostCenter array or be empty.

3. .pipe()

pipe()is a method of Observable used to chain operators together so that they are applied sequentially. In this code snippet, we will use filteroperators to filter the values ​​emitted by an Observable.

4. filter((costCenters) => !!costCenters)

This is an filterapplication of an operator that accepts a callback function as a parameter. The purpose of this callback function is to filter the values ​​emitted by the Observable. Let's break down this callback function:

  • (costCenters): This is the parameter of the callback function, which represents the value emitted by the Observable, which is the CostCenter array.
  • !!costCenters: This is costCentersthe double exclamation mark operator, which is used to coerce a value to a Boolean value. What the double exclamation mark does is convert a non-boolean value into a boolean value while ensuring that no nullOR is returned undefined. A double exclamation mark will be returned if costCentersit is a non-empty array true, otherwise false.

Therefore, filter((costCenters) => !!costCenters)the function is to filter out those values ​​​​that are not valid CostCenter arrays and only allow those non-empty arrays to pass.

Example

To better understand this code, let's look at an example. Assume userCostCenterService.getActiveCostCenters()it is a service method used to obtain the user's activity cost center and return an Observable. Here's an example of how to use this code:

import {
    
     Component } from '@angular/core';
import {
    
     Observable } from 'rxjs';
import {
    
     filter } from 'rxjs/operators';

@Component({
    
    
  selector: 'app-cost-center',
  template: `
    <div *ngIf="userCostCenters$ | async as costCenters; else loading">
      <h2>User Cost Centers:</h2>
      <ul>
        <li *ngFor="let costCenter of costCenters">{
     
     { costCenter.name }}</li>
      </ul>
    </div>
    <ng-template #loading>Loading...</ng-template>
  `,
})
export class CostCenterComponent {
    
    
  userCostCenters$: Observable<CostCenter[]>;

  constructor(private userCostCenterService: UserCostCenterService) {
    
    
    this.userCostCenters$ = this.userCostCenterService
      .getActiveCostCenters()
      .pipe(filter((costCenters) => !!costCenters));
  }
}

In the above example, userCostCenters$it is an Observable that getActiveCostCenters()obtains the user's activity cost center through a method. We then use asyncpipes to subscribe to the Observable in the template and use *ngIfdirectives to display the user's cost center or load messages based on the Observable's value.

filter((costCenters) => !!costCenters)Ensure that userCostCenters$the user's cost centers are displayed in the template only if a valid CostCenter array is included.

Summarize

The purpose of this Angular component code is to obtain the user's activity cost center and use filteroperators to filter out illegal values ​​to ensure that only valid CostCenter arrays will be sent to the template through the Observable. The double exclamation mark (!!) is used to coerce the value to a boolean while excluding nullor undefined, ensuring that only non-empty arrays pass the filter. This helps ensure that the template only displays content when valid data is available, improving app reliability and user experience.

Guess you like

Origin blog.csdn.net/i042416/article/details/132835924