admin管理员组文章数量:1399509
In my TypeScript
I have this class:
export class PhoneBookPerson {
Email: string;
Name: string;
Phonenumber: string;
ProfileImg: string;
Department: string;
JobTitle: string;
Username: string;
}
I'm wondering how can I check if any of the properties contains a specific value.
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);
}
}
In my TypeScript
I have this class:
export class PhoneBookPerson {
Email: string;
Name: string;
Phonenumber: string;
ProfileImg: string;
Department: string;
JobTitle: string;
Username: string;
}
I'm wondering how can I check if any of the properties contains a specific value.
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);
}
}
Share
Improve this question
edited May 5, 2017 at 9:24
mleko
12.3k7 gold badges52 silver badges71 bronze badges
asked Jan 25, 2017 at 12:54
user7255640user7255640
7
-
Where is
$SearchTerm
variable? what value you want to match with wich attribute of Person? – Bhushan Kawadkar Commented Jan 25, 2017 at 12:56 -
$SearchTerm contains any kind of value form a user input.
I would like it to see if any property in Person contains that value – user7255640 Commented Jan 25, 2017 at 12:58 - Just use Object.keys(person).some(k => k.includes($SearchTerm)) – AlexG Commented Jan 25, 2017 at 13:10
- I can not select includes – user7255640 Commented Jan 25, 2017 at 13:14
- And why cant you "select includes"? – robkuz Commented Jan 25, 2017 at 13:19
1 Answer
Reset to default 4Iterate over object properties and check if any of them contains specified text.
Sample function to do so (I assume only string properties in object)
function objectContains(obj, term: string): boolean {
for (let key in obj){
if (obj[key].indexOf(term) != -1) return true;
}
return false;
}
Example usage
if (objectContains(person, $SearchTerm)) {
// do something
}
本文标签: javascriptCheck if Object contains string in TypeScriptStack Overflow
版权声明:本文标题:javascript - Check if Object contains string in TypeScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744159270a2593258.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论