5

i have a dynamic data each time there could be dynamically different key value pair to be filtered in different data. how can we filter it with multiple key,value in lodash. i was using its filter function but result is not achievable.

example data:

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

]

and filter object is:

var filterby = {"PARTY":["REPUBLICAN","DEMOCRAT"],"BALLOT_STATUS":["PERM","POLL"]}

Filter function from lodash:

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

3 Answers 3

11

You could use plain Javascript and iterate the keys of the filterBy and the values.

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

Sign up to request clarification or add additional context in comments.

6 Comments

hello i know its too late to ask but i have using your code for filter but there was an error like .some is not a function, can you please help with this
@RahulDudharejiya, you need the same object structure for filterBy like above with properties and arrays as value, because you need the method from Array prototype. please add an example of the taken object.
@NinaScholz hi, its too late but how can it be modified if the Json array is nested one. here i want to filter using voter and statusVal. eg : [ { "VOTER" : 1012, "PARTY" : "REPUBLICAN", "PRECINCT" : 2408, "AGE_GROUP" : "71 +", "LAST_VOTED" : "08/2006", "YEARS_REG" : 51, "BALLOT_STATUS" : "PERM" "vehicleStatusHistory": { "id": 721, "Ind": "Y", "status": { "StatusId": 3, "StatusVal": "In-Transit" } }, } ]
it's quite a different format. please ask a new question along with wanted result and code, you tried.
Really worth solution .
|
4

_.filter method doesn't allow you to specify multiple options for filtering. Try with passing custom filter function:

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

3 Comments

Thanks @hsz, for your answer but filterby will be dynamic i will not know there could be 2 as well 3 or 5 key value pair for example there could be: var filterby = {"PARTY":["REPUBLICAN","DEMOCRAT"],"BALLOT_STATUS":["PERM","POLL"],"VOTER":"1014"}
So use variables instead.
how can you please guide me ?
0

The _.filter function which iterates over elements of collection which is passes on first parameter in your case it's data and second parameter should be a function which is invoked per iteration for more info see Documentation.

For this in your example you can use Arrow Functions

Please find below snippet for more info.

Change your filterby var as below

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.