我们首先创建字符串数组-
let studentDetails = [ {studentName: "John Smith"}, {studentName: "john smith"}, {studentName: "Carol Taylor"} ];
现在,匹配不区分大小写的子字符串,在其中使用filter()
和的概念toLowerCase()
。以下是代码-
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.
在这里,我的文件名为demo55.js。
输出结果
这将产生以下输出-
PS C:\Users\Amit\JavaScript-code> node demo55.js [ { studentName: 'John Smith' }, { studentName: 'john smith' } ]