[英]How to make filters case insensitive in Vuejs/Javascript
I'm trying to build an application in Vuejs 2
where I'm having a filter to find out the specific data from an array of elements. 我正在尝试在Vuejs 2
中构建一个应用程序,我有一个过滤器来查找元素数组中的特定数据。 This filter is case sensitive
but I want to have case insensitive
, following is my code: 此过滤器case sensitive
但我希望case insensitive
,以下是我的代码:
tableFilter: function () {
if( this.model ) {
return this.model.filter(
item =>
item.clients_association.includes( this.search_by_name ) &&
item.event_type.includes( this.search_by_event_type )
)
.map(
d => (
{
id: d.id,
client_names: d.clients_association,
stellar_participants: d.stellar_participants,
contact_participants: d.contacts_association,
}
)
);
}
}
Please guide me how can I achieve it. 请指导我如何实现它。
Since Array#includes
is case sensitive you can convert everything to lower case: 由于Array#includes
是区分大小写的,因此您可以将所有内容转换为小写:
const searchTerm = this.search_by_name.toLowerCase();
const searchEvent = this.search_by_event_type.toLowerCase();
this.model.filter((item) =>
item.clients_association.toLowerCase().includes(searchTerm) &&
item.event_type.toLowerCase().includes(searchEvent)
);
You can also use String#test
, which accepts a regex with the insensitive (i) flag, and returns true
if the string matches the expression, or false
if not: 您还可以使用String#test
,它接受带有insensitive(i)标志的正则表达式,如果字符串与表达式匹配则返回true
否则返回false
:
const searchTerm = new RegExp(this.search_by_name, 'i');
const searchEvent = new RegExp(this.search_by_event_type, 'i');
this.model.filter((item) =>
searchTerm.test(item.clients_association) &&
searchEvent.test(item.event_type)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.