noclist
noclist

Reputation: 1819

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));

Upvotes: 1

Views: 1396

Answers (2)

alotropico
alotropico

Reputation: 1994

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)

Upvotes: 1

Md. Jahidul Islam
Md. Jahidul Islam

Reputation: 316

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

Upvotes: 1

Related Questions