在我的TypeScript
我有这个课程:
export class PhoneBookPerson {
Email: string;
Name: string;
Phonenumber: string;
ProfileImg: string;
Department: string;
JobTitle: string;
Username: string;
}
我想知道如何检查是否有任何属性包含特定值。
let $SearchTerm = target.val();
function RetrievedUsers(sender: any, args: any) {
for (let i = 0; i < users.get_count(); i++) {
let user = users.getItemAtIndex(i);
let person = new PhoneBookPerson();
person.Name = user.get_loginName();
person.Email = user.get_email();
person.Username = user.get_loginName();
person.JobTitle = user.get_title();
<-- search of person contains value from $SearchTerm
usermatch.push(person);
}
}
答案 0 :(得分:2)
迭代对象属性并检查它们中是否包含指定的文本。
要执行此操作的示例函数(我假设对象中只有字符串属性)
function objectContains(obj, term: string): boolean {
for (let key in obj){
if(obj[key].indexOf(term) != -1) return true;
}
return false;
}
使用示例
if (objectContains(person, $SearchTerm)) {
// do something
}