【问题标题】:React: Filter function case insensitiveReact:过滤函数不区分大小写
【发布时间】:2018-02-26 00:24:44
【问题描述】:

我正在过滤一个 Json 文件。如何进行不区分大小写的搜索?

这是我的代码

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

【问题讨论】:

  • hotel.name.toLowerCase().includes(inputNameValue.toLowerCase())?
  • 成功了,非常感谢
  • 不要在每次迭代时都这样做。看我的回答

标签: reactjs filter case-insensitive


【解决方案1】:

如何规范两个字符串的大小写,例如将它们都小写:
请注意,建议在过滤器之外更改输入。

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

【讨论】:

    【解决方案2】:

    执行此案:

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

    【讨论】:

      猜你喜欢
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 2016-03-15
      • 2018-07-10
      相关资源
      最近更新 更多