-1

In my angualar project I have a table with a list of users and the search bar. Here is my search function:

searchUser() {
    const regExp = new RegExp(this.searchTerm, 'gi');

    this.users = this.users.filter((user) => regExp.test(user.username));
}

Now its searching only by username, but I want to be able to search by selected field. I have a select with options of my search criterion. So I dont know how dynamicly change property of user object inside filter fuction in according to selected search criterion.

I've thought about es6 template strings and tried like this:

this.users = this.users.filter((user) => regExp.test(user`.${this.selectedCriterion}`)

But it didn't work. What is the proper syntax for this? Will appreciate any advise

Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82
Alex Bryanskiy
  • 395
  • 1
  • 3
  • 19

1 Answers1

0

Use computed property syntax,

this.users = this.users.filter((user) => regExp.test(user[this.selectedCriterion])
Anurag Awasthi
  • 6,115
  • 2
  • 18
  • 32