Search code examples
reactjsfiltercase-insensitive

React: Filter function case insensitive


I am filtering with react over a Json file. How can I do a case insensitive search?

This is my code

_handleSearch ({ inputNameValue, inputPriceValue }) {
  let list = data.filter(hotel => hotel.name.includes(inputNameValue))
  }

Solution

  • How bout case normalizing both strings, for example lower case them both:
    Note that its advised to change the input once outside the filter.

    _handleSearch ({ inputNameValue, inputPriceValue }) {
      const lowerCased = inputNameValue.toLowerCase();
      let list = data.filter(hotel => hotel.name.toLowerCase().includes(lowerCased ))
      }