0

I have created a pipe which filters data based on user input search in angular 2.

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'filterName',
  pure:false
})
export class FilterNamePipe implements PipeTransform {

  transform(items: any[], searchTerm: any): any[] {
    if (!searchTerm) return items;
    return items.filter(function(item){
      if(item.fullNme == null){
        return null;
      }else {
        return item.fullNme.toLowerCase().includes(searchTerm.toLowerCase());
      }
    })
  }

}

JSON : 
{ name: "Peter Martha",   age: 2 },
{ name: "Martha, Pablo ",   age: 55 },
{ name: "Linda Peter",   age: 20 },

The above filter works fine.I want one which filters based on first input string.When i look for "P/p" it should display only Peter Martha , but not Linda Peter or Martha Pablo.Can some one tell what am i doing wrong or what changes should be done.Thanks in advance.

Aniruddha Das
  • 20,520
  • 23
  • 96
  • 132
OptimusPrime
  • 85
  • 2
  • 4
  • 12

1 Answers1

1

I would use the String.startsWith() function

return item.fullNme.toLowerCase().startsWith(searchTerm.toLowerCase());
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165