这个问题类似于这一个
Jquery filter array of object with loop,但这次我需要使用一组对象进行过滤.
例:
myArray = [ { userid: "100",projectid: "10",rowid: "0" },{ userid: "101",projectid: "11",rowid: "1"},{ userid: "102",projectid: "12",rowid: "2"},{ userid: "103",projectid: "13",rowid: "3" },{ userid: "101",rowid: "4" } ...]
我想用这样的数组过滤它:
myFilter = [ { userid: "101",projectid: "11" },{ userid: "102",projectid: "12" },{ userid: "103",projectid: "11" }]
并返回此(myFilter中的userid和projectid需要匹配myArray中的userid和projectid):
myArrayFiltered = [ { userid: "101",rowid: "1" },rowid: "2" }]
我怎样才能做到这一点 ?
解决方法
var filtered = []; for(var arr in myArray){ for(var filter in myFilter){ if(myArray[arr].userid == myFilter[filter].userid && myArray[arr].projectid == myFilter[filter].projectid){ filtered.push(myArray[arr].userid); } } } console.log(filtered);