Search code examples
angularjstypescriptaureliatypescript2.0

Typescript filter returning empty list


I am trying to filter an array in typescript and aurelia however I only ever get empty lists.

If i have the key word of ra and search on the firstName property i expect to have the first object (with name of "Raja") returned. I dont understand what I am missing?

Below is an array of contact objects.

let contacts = [
  {
    id:getId(),
    firstName:"Raja",
    lastName:"Mani",
    email:"[email protected]",
    phoneNumber:"408-973-5050",
    birthDate: new Date(1973, 5, 1)
  },
  {
    id:getId(),
    firstName:"Jhansi",
    lastName:"Rani",
    email:"[email protected]",
    phoneNumber:"867-5309",
    birthDate: new Date(1970, 5, 24)
  },
  {
    id:getId(),
    firstName:"Aditi",
    lastName:"Raja",
    email:"[email protected]",
    phoneNumber:"408-973-9006",
    birthDate: new Date(2001, 10, 12)
  }        
];

These objects are have the interface IContact

export interface IContact {
    id: number;
    firstName: string;
    lastName: string;
    email: string;
    phoneNumber: string;
    birthDate: Date;
}

And this is my filter query

let results = contacts.filter((c: IContact) => ((c.firstName.indexOf(keyword) !== -1)));

Solution

  • As indexOf is case sensitive you need transform both case before comparing:

    c.firstName.toUpperCase().indexOf(keyword.toUpperCase()) !== -1