gpt4 book ai didi

javascript - 使用lodash过滤json数组中具有多个键的多个值

转载 作者:行者123 更新时间:2023-12-01 02:05:02 24 4
gpt4 key购买 nike

我每次都有一个动态数据,可以动态地在不同的数据中过滤不同的键值对。我们如何在 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);

最佳答案

您可以使用纯 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; }

关于javascript - 使用lodash过滤json数组中具有多个键的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45236312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com