ใช้ filter() ร่วมกับ toLowerCase() และ indexOf() เพื่อค้นหาสตริงย่อยไม่สนใจตัวพิมพ์เล็ก/ใหญ่
ตัวอย่างโค้ด
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
));
วิธีรันโปรแกรม
ใช้คำสั่ง node fileName.js (เช่น node demo55.js)
ผลลัพธ์
[
{ studentName: 'John Smith' },
{ studentName: 'john smith' }
]