我有一些动态数据,每次可能有不同的键值对需要在不同的数据中进行过滤。如何使用lodash中的多个键值来过滤它?我正在使用它的filter函数,但结果无法实现。
示例数据:
过滤器对象是:
示例数据:
var data = [ {
"VOTER" : 1012,
"PARTY" : "REPUBLICAN",
"PRECINCT" : 2408,
"AGE_GROUP" : "71 +",
"LAST_VOTED" : "08/2006",
"YEARS_REG" : 51,
"BALLOT_STATUS" : "PERM"
}, {
"VOTER" : 1013,
"PARTY" : "REPUBLICAN",
"PRECINCT" : 2411,
"AGE_GROUP" : "71 +",
"LAST_VOTED" : "08/2006",
"YEARS_REG" : 50,
"BALLOT_STATUS" : "PERM"
}, {
"VOTER" : 1014,
"PARTY" : "DEMOCRAT",
"PRECINCT" : 2424,
"AGE_GROUP" : "71 +",
"LAST_VOTED" : "08/2006",
"YEARS_REG" : 50,
"BALLOT_STATUS" : "PERM"
}, {
"VOTER" : 1015,
"PARTY" : "DEMOCRAT",
"PRECINCT" : 2418,
"AGE_GROUP" : "71 +",
"LAST_VOTED" : "08/2006",
"YEARS_REG" : 50,
"BALLOT_STATUS" : "POLL"
},{
"VOTER" : 1109,
"PARTY" : "AMERICAN INDEP",
"PRECINCT" : 2404,
"AGE_GROUP" : "71 +",
"LAST_VOTED" : "08/2006",
"YEARS_REG" : 34,
"BALLOT_STATUS" : "POLL"
},{
"VOTER" : 1111,
"PARTY" : "DECLINED",
"PRECINCT" : 2414,
"AGE_GROUP" : "71 +",
"LAST_VOTED" : "08/2006",
"YEARS_REG" : 34,
"BALLOT_STATUS" : "POLL"
}
]
过滤器对象是:
var filterby = {"PARTY":["REPUBLICAN","DEMOCRAT"],"BALLOT_STATUS":["PERM","POLL"]}
lodash中的过滤函数:var filtered_data = _.filter(data, filterby);
filterBy
方法,包含属性和数组作为值,因为你需要使用Array
原型中的方法。请添加一个示例所取的对象。 - Nina Scholz