Sử dụng filter() kết hợp toLowerCase() và includes() hoặc indexOf() để tìm kiếm không phân biệt chữ hoa/thường.
Ví dụ
let studentDetails = [
{studentName: "John Smith"},
{studentName: "john smith"},
{studentName: "Carol Taylor"}
];
var searchName = "John Smith";
// Lọc không phân biệt hoa/thường
var result = studentDetails.filter(obj =>
obj.studentName.toLowerCase().indexOf(searchName.toLowerCase()) >= 0
);
console.log(result);
Cách chạy
node demo.js
Kết quả
[
{ studentName: 'John Smith' },
{ studentName: 'john smith' }
]
Cách hiện đại hơn (ES2016+): Dùng includes() thay indexOf() >= 0:
obj.studentName.toLowerCase().includes(searchName.toLowerCase())