打字稿过滤器返回空列表
Typescript filter returning empty list
我正在尝试过滤 typescript 和 aurelia 中的数组,但我只得到空列表。
如果我有关键字 ra
并在 firstName
属性 上搜索,我希望返回第一个对象(名称为 "Raja")。我不明白我错过了什么?
下面是 contact
个对象的数组。
let contacts = [
{
id:getId(),
firstName:"Raja",
lastName:"Mani",
email:"rmani@gmail.com",
phoneNumber:"408-973-5050",
birthDate: new Date(1973, 5, 1)
},
{
id:getId(),
firstName:"Jhansi",
lastName:"Rani",
email:"jrani@gmail.com",
phoneNumber:"867-5309",
birthDate: new Date(1970, 5, 24)
},
{
id:getId(),
firstName:"Aditi",
lastName:"Raja",
email:"araja@gmail.com",
phoneNumber:"408-973-9006",
birthDate: new Date(2001, 10, 12)
}
];
这些对象具有接口 IContact
export interface IContact {
id: number;
firstName: string;
lastName: string;
email: string;
phoneNumber: string;
birthDate: Date;
}
这是我的过滤查询
let results = contacts.filter((c: IContact) => ((c.firstName.indexOf(keyword) !== -1)));
由于 indexOf
区分大小写,您需要在比较之前转换两种大小写:
c.firstName.toUpperCase().indexOf(keyword.toUpperCase()) !== -1
我正在尝试过滤 typescript 和 aurelia 中的数组,但我只得到空列表。
如果我有关键字 ra
并在 firstName
属性 上搜索,我希望返回第一个对象(名称为 "Raja")。我不明白我错过了什么?
下面是 contact
个对象的数组。
let contacts = [
{
id:getId(),
firstName:"Raja",
lastName:"Mani",
email:"rmani@gmail.com",
phoneNumber:"408-973-5050",
birthDate: new Date(1973, 5, 1)
},
{
id:getId(),
firstName:"Jhansi",
lastName:"Rani",
email:"jrani@gmail.com",
phoneNumber:"867-5309",
birthDate: new Date(1970, 5, 24)
},
{
id:getId(),
firstName:"Aditi",
lastName:"Raja",
email:"araja@gmail.com",
phoneNumber:"408-973-9006",
birthDate: new Date(2001, 10, 12)
}
];
这些对象具有接口 IContact
export interface IContact {
id: number;
firstName: string;
lastName: string;
email: string;
phoneNumber: string;
birthDate: Date;
}
这是我的过滤查询
let results = contacts.filter((c: IContact) => ((c.firstName.indexOf(keyword) !== -1)));
由于 indexOf
区分大小写,您需要在比较之前转换两种大小写:
c.firstName.toUpperCase().indexOf(keyword.toUpperCase()) !== -1