Home > Software design >  how to create a filter pipe by number in angular
how to create a filter pipe by number in angular

Time:08-03

i want to create a filter pipe, which i can filter by number, because I will use it to filter by salary. I created the filter for string characters, but I don't know how it would be with numbers, because I'm new in this.

here is my pipe

import { Pipe, PipeTransform } from '@angular/core';
import { Jobs } from '../interfaces/jobs.interface';

@Pipe({
  name: 'filterSalary'
})
export class FilterSalaryPipe implements PipeTransform {

  transform(value: Array<Jobs>, arg: any): any {
    const resultPosts = [];
    for (const jobs of value) {
      if (jobs.salary.indexOf(arg) > -1) {
        resultPosts.push(jobs);
      };
    };
    return resultPosts;
  }
}

in the jobs.salary what method, am i supposed to use?

CodePudding user response:

You can check your string which is number or not by using typeof().
eg.

 const searchString = 11;
 if(typeof(searchString) === 'number'){
   console.log(searchString);
 }

For your case, it would be

import { Pipe, PipeTransform } from '@angular/core';
import { Jobs } from '../interfaces/jobs.interface';

@Pipe({
  name: 'filterSalary'
})
export class FilterSalaryPipe implements PipeTransform {

  transform(value: Array<Jobs>, arg: any): any {
    const resultPosts = [];
    for (const jobs of value) {
      if (typeof(jobs.salary) === 'number') {
       resultPosts.push(jobs.salary.toString().toLowerCase().includes(arg));
      };
    };
    return resultPosts;
  }
}
  • Related