它可以像这样通用吗?
function filterArray(inputArray,customFunction){
return $.grep(inputArray, function(e){ return customFunction(e); });
}
wherecustomFunction
可以是用户定义的函数来限定对象为选中状态
例子 :
var sampleArray = [{name:"Ahamed",age: 21},
{name:"AhamedX",age: 21},
{name:"Babu",age: 25},
{name:"Mustafa",age: 27} ];
function nameComparator(obj){
return obj["name"]=="Ahamed";
}
function ageFilter(obj){
return obj["age"]>=25;
}
var filteredArray=filterArray(sampleArray,nameComparator);
alert(filteredArray.length);
var filteredArray=filterArray(sampleArray,ageFilter);
alert(filteredArray.length);
小提琴链接:http: //jsfiddle.net/MAq6c/