how to write a custom pipe for filtering the value if length of input text enter is more than 2 charcters

462 Views Asked by At

I have this function where I will display array values , if value entered in input box is more than 1 , it will compare and display value. how can i rewrite this in custom filter.

  users: searchInputModel[] = [
    {
      name: 'Jack Daniel',
      code: 'AD1213'
    },
    {
      name: 'John Doe',
      code: 'TFQUN'

    }
    ];

constructor() {
    this.userControl.valueChanges.subscribe((user) => {
      if (user && user.length > 1) {
        this.filteredUsers = user ? this.filterUsers(user) : this.users.slice();
      }
    });
  }

  filterUsers(value: string): searchInputModel[] {
    const filterValue = value.toLowerCase();
    return this.users.filter(user => user.name.toLowerCase().indexOf(filterValue) === 0);
  }
1

There are 1 best solutions below

0
On

You can try:

this.userControl.valueChanges.pipe(
   filter(user => user && user.length > 1),
   map(e => this.filterUsers(e))
).subscribe(filteredUsers => this.filteredUsers = filteredUsers);