2

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);
    }
}
7
  • Where is $SearchTerm variable? what value you want to match with wich attribute of Person? 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 Commented Jan 25, 2017 at 12:58
  • Just use Object.keys(person).some(k => k.includes($SearchTerm)) Commented Jan 25, 2017 at 13:10
  • I can not select includes Commented Jan 25, 2017 at 13:14
  • And why cant you "select includes"? Commented Jan 25, 2017 at 13:19

1 Answer 1

4

Iterate 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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.