Search code examples
vue.jsquasar

filter a list of posts accepting lowerCase or Uppercase strings


I intend to filter a list of posts by the title.

The logic is already implemented and works.

The problem that remains to be solved is to allow the filtering be done regardless of whether the strings contain upper or lower case letters.

For example: a string with the word Today should be able to be filtered in input by lowerCase, i.e., today

inputFilter:function(){
      var vm = this;
      return vm.posts.filter((post) => {
           return post.title.match(searchValue);
      });
 }

How to solve this?


Solution

  • You can use either the toUpperCase() or toLowerCase() javascript functions :)

    Whichever function you use, be sure to apply it to both sides of the match statement. Otherwise it will not match if the non transformed side includes a mix of upper case and lower case characters.

    inputFilter:function() {
       var vm = this;
       return vm.posts.filter((post) => {
          return post.title.toUpperCase().match(searchValue.toUpperCase());
       });
    }
    
    inputFilter:function() {
       var vm = this;
       return vm.posts.filter((post) => {
          return post.title.toLowerCase().match(searchValue.toLowerCase());
       });
    }
    

    MDN Web Docs - String.prototype.toUpperCase()

    MDN Web Docs - String.prototype.toLowerCase()