1

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));
noclist
  • 1,659
  • 2
  • 25
  • 66
  • 1
    Does this answer your question? [javascript includes() case insensitive](https://stackoverflow.com/questions/48145432/javascript-includes-case-insensitive) – xandermonkey Jul 09 '20 at 19:57

2 Answers2

1
this.employeeDisplayList = this.employeeList.filter(e => {
    const emp = e.employeeName.toLowerCase();
    const str = this.searchStr.toLowerCase();
    return emp.includes(str);
});
1

You can just apply .toLowerCase() to both the employeeName and the 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)

alotropico
  • 1,904
  • 3
  • 16
  • 24