JavaScript 过滤字符串数组,匹配不区分大小写的子字符串?

javascriptweb developmentobject oriented programming更新于 2025/11/6 7:08:17

我们先创建字符串数组 −

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' } ]

相关文章


有用资源