ES6:使用不区分大小写的术语过滤数据

ES6: Filter data with case insensitive term

这就是我按标题值筛选 data 的方式:

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

所以数据像

Sample one
Sample Two
Bla two

将是 'reduced' 到

Bla two

如果我按 two 筛选。

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

Sample Two
Bla two

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

// 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()))