使用lodash在json数组中使用多个键过滤多个值

时间:2017-07-21 11:18:14

标签: javascript jquery json lodash

每次可以在不同数据中过滤动态不同的键值对时,我都会有动态数据。我们如何在lodash中使用多个键值来过滤它。我正在使用其过滤功能,但结果无法实现。

示例数据:

    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);

3 个答案:

答案 0 :(得分:3)

您可以使用普通的Javascript并迭代filterBy的键和值。



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" }],
    filterBy = { PARTY: ["REPUBLICAN", "DEMOCRAT"], BALLOT_STATUS: ["PERM", "POLL"] },
    result = data.filter(function (o) {
        return Object.keys(filterBy).every(function (k) {
            return filterBy[k].some(function (f) {
                return o[k] === f;
            });
        });
    });

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




ES6



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" }],
    filterBy = { PARTY: ["REPUBLICAN", "DEMOCRAT"], BALLOT_STATUS: ["PERM", "POLL"] },
    result = data.filter(o => Object.keys(filterBy).every(k => filterBy[k].some(f => o[k] === f)));

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:1)

_.filter方法不允许您指定多个过滤选项。尝试传递自定义过滤功能:

_.filter(data, function (item) {
  return ['REPUBLICAN', 'DEMOCRAT'].indexOf(item.PARTY) >= 0
      && ['PERM', 'POLL'].indexOf(item.BALLOT_STATUS) >= 0
})

答案 2 :(得分:0)

_.filter函数迭代遍历第一个参数的集合元素,它是data和第二个参数应该是每次迭代调用的函数以获取更多信息见Documentation

在您的示例中,您可以使用Arrow Functions

请在下面的摘要中找到更多信息。

将您的filterby var更改为

var filterby = (m) => { return m => m.PARTY == "REPUBLICAN" || m.PARTY == "DEMOCRAT" && m.BALLOT_STATUS == "PERM" || m.BALLOT_STATUS == "POLL"  };



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"]}

var filterby = (m) => { return m => m.PARTY == "REPUBLICAN" || m.PARTY == "DEMOCRAT" && m.BALLOT_STATUS == "PERM" || m.BALLOT_STATUS == "POLL"  };


var filtered_data = _.filter(data, filterby);
console.log(filtered_data);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
&#13;
&#13;
&#13;