我正在使用jQuery的“grep”函数来过滤数据,下面的代码示例在条件静态时工作正常。
但是,如果“name”和“school”参数有多个值如何过滤它??,请考虑我收到名称&学校从多个复选框list.Ideally我不想硬编码
var students =
[ {'id':1, 'name':'amit', 'school':'st john'},
{'id':2, 'name':'ankit','school':'st mary'},
{'id':3, 'name':'john', 'school':'hill school'},
{'id':4, 'name':'matt', 'school':'st john'}];
jQuery.grep(students,function(student)
{ return student.name =='john' && student.school=='st.john' });
//something like this is possible?
{ student.name =='john||jack||stella' && student.school=='st.john||st.mary' });
答案 0 :(得分:3)
你可以这样做,但不像你想的那样:
var names = [ 'john', 'jack', 'stella' ];
var schools = [ 'st john', 'st mary' ];
var matches = jQuery.grep(students, function(student) {
return names.indexOf(student.name) != -1
&& schools.indexOf(student.school) != -1;
});
因此,您只需根据动态输入设置names
和schools
数组即可。