以下内容仅供参考:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>js数组对象 模糊查找 指定 匹配字段内容 简单案例</title>
</head>
<body>
</body>
<script type="text/javascript">
// 需要查找的数据集
let tableList = [
{
Name: "张三",
CreateTime: "2023-06-03 16:29:10",
Company: "资讯部"
},
{
Name: "李四",
CreateTime: "2023-06-04 08:35:21",
Company: "资讯部"
},
{
Name: "王五",
CreateTime: "2023-06-04 20:53:34",
Company: "财务部"
},
{
Name: "赵六",
CreateTime: "2023-07-05 16:30:42",
Company: "人事部"
},
{
Name: "张三",
CreateTime: "2023-07-05 16:50:42",
Company: "集团总部"
}
]
/**
* list 数据源
* keyWord 关键字
* attribute 需要查找的字段
*/
function textQuery(list, keyWord, attribute = []) {
const reg = new RegExp(keyWord) // 创建正则表达式
const arr = []
for (let i = 0; i < list.length; i++) {
let isFind = false
for (let j = 0; j < attribute.length; j++) {
if (list[i][attribute[j]]) {
isFind = reg.test(list[i][attribute[j]])
if (isFind) {
arr.push(list[i])
break
}
}
}
}
console.log(arr)
return arr
}
//打开控制台查看结果
textQuery(tableList, '张三', ['Name', 'Company'])
</script>
</html>
运行结果: