[英]javascript filter an array of strings, matching case insensitive substring
I have an array of names filtered by a certain string.我有一个由某个字符串过滤的名称数组。 How can I match all names that include this string, but insensitive of case?
如何匹配包含此字符串但不区分大小写的所有名称?
this.employeeDisplayList = this.employeeList.filter(e => e.employeeName.includes(this.searchStr));
this.employeeDisplayList = this.employeeList.filter(e => {
const emp = e.employeeName.toLowerCase();
const str = this.searchStr.toLowerCase();
return emp.includes(str);
});
You can just apply .toLowerCase()
to both the employeeName and the searchString.您可以将
.toLowerCase()
应用于employeeName 和searchString。
let employeeDisplayList = [{employeeName: "Jules 1"}, {employeeName: "jules 2"}, {employeeName: "Max"}]; let searchStr = "Jules"; console.log(employeeDisplayList.filter(e => e.employeeName.toLowerCase().indexOf(searchStr.toLowerCase()) >= 0));
Another way would be to search with a case insensitive regex: string.match(/word/i)
另一种方法是使用不区分大小写的正则表达式进行搜索:
string.match(/word/i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.