【记录55】多个参数查询,无用参数不记查询
let students = [
{name: '姓名', age: '年龄', sex: '性别', grade: '年级', location: '家庭地址', phone: '联系方式', studentID: '学号', enrollmentDate: '入学日期', major:'', class: '班级', performance: '成绩', rank: '排名', evaluation: '评价'},
...
{name: '姓名', age: '年龄', sex: '性别', grade: '年级', location: '家庭地址', phone: '联系方式', studentID: '学号', enrollmentDate: '入学日期', major:'', class: '班级', performance: '成绩', rank: '排名', evaluation: '评价'},
]
页面有多个输入框条件查询,只输入年龄、年级、性别 为查询条件
cosnt findobj = { name: '', age: '', sex: '', grade: '', location: '', phone: '', studentID: '', enrollmentDate: '', major:'', class: '', performance: '', rank: '', evaluation: '' }
const resultobj; // 新对象
for(let key in finobj) {
if( findobj[key] ){
//验证该属性是否为真
resultobj[key] = findobj[key];
}
}
console.log(resultobj); // 结果:{age:'12', sex:'男', grade:'大一'}
过滤出了只需要查询的条件
let list = [];
for(let index=0; index < students.length; index++) {
const item = students[index];
const reg = new new RegExp(resultobj [key ]);
for(let key in resultobj) {
if(reg.test(item[key])) {
list.push(item)
}
}
}
console.log(list);