Đầu tiên chúng ta hãy tạo một mảng chuỗi -
let studentDetails =
[
{studentName: "John Smith"},
{studentName: "john smith"},
{studentName: "Carol Taylor"}
]; Bây giờ, đối sánh chuỗi con không phân biệt chữ hoa chữ thường, sử dụng bộ lọc () và khái niệm toLowerCase () trong đó. Tiếp theo là mã -
Ví dụ
let studentDetails =
[
{studentName: "John Smith"},
{studentName: "john smith"},
{studentName: "Carol Taylor"}
];
var searchName="John Smith"
console.log(studentDetails.filter(obj =>
obj.studentName.toLowerCase().indexOf(searchName.toLowerCase()) >= 0)); Để chạy chương trình trên, bạn cần sử dụng lệnh sau;
node fileName.js.
Đây, tên tệp của tôi là demo55.js.
Đầu ra
Điều này sẽ tạo ra kết quả sau -
PS C:\Users\Amit\JavaScript-code> node demo55.js
[ { studentName: 'John Smith' }, { studentName: 'john smith' } ]