【问题标题】:ES6: Filter data with case insensitive termES6:使用不区分大小写的术语过滤数据
【发布时间】:2017-11-12 03:58:23
【问题描述】:

这就是我如何按标题值过滤一些data

data.filter(x => x.title.includes(term))

所以数据像

Sample one
Sample Two
Bla two

将被“缩减”为

Bla two

如果我按two 过滤。

但我需要得到过滤后的结果

Sample Two
Bla two

【问题讨论】:

    标签: javascript filter ecmascript-6 case-insensitive


    【解决方案1】:

    您可以使用不区分大小写的正则表达式:

    // Note that this assumes that you are certain that `term` contains
    // no characters that are treated as special characters by a RegExp.
    data.filter(x => new RegExp(term, 'i').test(x.title));
    

    一种可能更简单、更安全的方法是将字符串转换为小写并进行比较:

    data.filter(x => x.title.toLowerCase().includes(term.toLowerCase()))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多