ES6:使用不区分大小写的术语过滤数据 [英] ES6: Filter data with case insensitive term
本文介绍了ES6:使用不区分大小写的术语过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我通过标题值过滤某些data
的方法:
This is how I filter some data
by title value:
data.filter(x => x.title.includes(term))
像这样的数据
Sample one
Sample Two
Bla two
将被简化为"
Bla two
如果我按two
进行过滤.
但是我需要得到过滤后的结果
But I need to get the filtered result
Sample Two
Bla two
推荐答案
您可以使用不区分大小写的正则表达式:
You can use a case-insensitive regular expression:
// 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));
一种可能更简单,更安全的方法是将字符串转换为小写并进行比较:
A perhaps easier and safer approach is to convert the strings to lowercase and compare:
data.filter(x => x.title.toLowerCase().includes(term.toLowerCase()))
这篇关于ES6:使用不区分大小写的术语过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文