Search filter with JS

Asked

Viewed 29 times

0

I am making a search filter with Vue.js and in my filter I would like to search name and email, but I only managed to do with the name of the user, how to put another "argument".

The code is like this

computed: {
    filteredUsers: function(){
      return this.userLists.filter((userList) => {
        return userList.usuario.toLowerCase().match(this.search.toLowerCase());
      });
    }
  },

I wish it were something like

return userList.usuario.toLowerCase().match(this.search.toLowerCase()) || userList.email.toLowerCase().match(this.search.toLowerCase()) ;

1 answer

1


You’re already in the filter iteration so you can do what you want! I suggest changing the name of the internal variable of the userLists filter to another as item, because what it receives is each item of your this.userLists and putting the same name generates confusion, but rest of to do what you want.

computed: {
    filteredUsers: function(){
      return this.userLists.filter((item) => {
        return (
                 item.usuario.toLowerCase().match(this.search.toLowerCase()) ||
                 item.email.toLowerCase().match(this.search.toLowerCase())
               );
      });
    }
  },

Browser other questions tagged

You are not signed in. Login or sign up in order to post.