简体   繁体   中英

Check if object contains value from array using lodash

How can I filter Objects based on value from another array the object is as follows:

const myJson = {

    "2020-01-10": [
      {
        "id": 1,
        "region": "MH",
        "category": "demo",
        "country": "India",
        "descp": "due date for filing GSTR-7",
        "applicableto": "",
        "duedate": "2020-01-10",
        "previousdate": null
      },
      {
        "id": 2,
        "region": "MH2",
        "category": "demo",
        "country": "India2",
        "descp": "due date for filing GSTR-7",
        "applicableto": "",
        "duedate": "2020-01-10",
        "previousdate": null
      }
    ],
    "2020-01-28": [
      {
        "id": 3,
        "region": "GJ",
        "category": "test",
        "country": "India",
        "descp": "GSTR-11 Return for details of goods and services purchased in India",
        "applicableto": "",
        "duedate": "2020-01-28",
        "previousdate": null
      },
      {
        "id": 3,
        "region": "MH",
        "category": "test",
        "country": "India",
        "descp": "GSTR-11 Return for details of goods and services purchased in India",
        "applicableto": "",
        "duedate": "2020-01-28",
        "previousdate": null
      }
    ]
}

and my region array consists of

regionArr=['MH','MH2'].

I want to filter the above object by checking the whether the regionArr elements are mentioned in any of the object for the key region using lodash. I tried

._every(this.regionArr, el =>
          ._includes(event.region, el)
);

but it's not working. I'm expecting an output like this

const myJson = {

    "2020-01-10": [
      {
        "id": 1,
        "region": "MH",
        "category": "demo",
        "country": "India",
        "descp": "due date for filing GSTR-7",
        "applicableto": "",
        "duedate": "2020-01-10",
        "previousdate": null
      },
      {
        "id": 2,
        "region": "MH2",
        "category": "demo",
        "country": "India2",
        "descp": "due date for filing GSTR-7",
        "applicableto": "",
        "duedate": "2020-01-10",
        "previousdate": null
      }
    ],
    "2020-01-28": [

      {
        "id": 3,
        "region": "MH",
        "category": "test",
        "country": "India",
        "descp": "GSTR-11 Return for details of goods and services purchased in India",
        "applicableto": "",
        "duedate": "2020-01-28",
        "previousdate": null
      }
    ]
}

This function iterates an object with _.mapValues() , and use _.intersectionWith() to filter the array from the object, with a 2nd array of values, and uses a comparator function to match items:

 const fn = (obj, arr, comparator) => _.mapValues( obj, v => _.intersectionWith(v, arr, comparator) ) const myJson = {"2020-01-10":[{"id":1,"region":"MH","category":"demo","country":"India","descp":"due date for filing GSTR-7","applicableto":"","duedate":"2020-01-10","previousdate":null},{"id":2,"region":"MH2","category":"demo","country":"India2","descp":"due date for filing GSTR-7","applicableto":"","duedate":"2020-01-10","previousdate":null}],"2020-01-28":[{"id":3,"region":"GJ","category":"test","country":"India","descp":"GSTR-11 Return for details of goods and services purchased in India","applicableto":"","duedate":"2020-01-28","previousdate":null},{"id":3,"region":"MH","category":"test","country":"India","descp":"GSTR-11 Return for details of goods and services purchased in India","applicableto":"","duedate":"2020-01-28","previousdate":null}]} const regionArr = ['MH','MH2'] const result = fn(myJson, regionArr, (a, b) => a.region === b) console.log(result)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

If you want to support multiple, you'll need to add a comparator function for each array. Pass tuples of [array, comparator], and collect them to an array of filters using rest params. Reduce the array of filters, and use the current array from the object ( v ) as the initial value. On each iteration intersect the current array ( acc ) with the the filter array, using the comparator.

 const fn = (obj, ...filters) => _.mapValues( obj, v => filters.reduce((acc, [arr, comparator]) => _.intersectionWith(acc, arr, comparator) , v) ) const myJson = {"2020-01-10":[{"id":1,"region":"MH","category":"demo","country":"India","descp":"due date for filing GSTR-7","applicableto":"","duedate":"2020-01-10","previousdate":null},{"id":2,"region":"MH2","category":"demo","country":"India2","descp":"due date for filing GSTR-7","applicableto":"","duedate":"2020-01-10","previousdate":null}],"2020-01-28":[{"id":3,"region":"GJ","category":"test","country":"India","descp":"GSTR-11 Return for details of goods and services purchased in India","applicableto":"","duedate":"2020-01-28","previousdate":null},{"id":3,"region":"MH","category":"test","country":"India","descp":"GSTR-11 Return for details of goods and services purchased in India","applicableto":"","duedate":"2020-01-28","previousdate":null}]} const regionArr = ['MH','MH2'] const categoryArr = ['demo'] const result = fn( myJson, [regionArr, (a, b) => a.region === b], [categoryArr, (a, b) => a.category === b] ) console.log(result)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

This is not a lodash solution but plain JS, hope it helps.

 const myJson = { "2020-01-10": [ { "id": 1, "region": "MH", "category": "demo", "country": "India", "descp": "due date for filing GSTR-7", "applicableto": "", "duedate": "2020-01-10", "previousdate": null }, { "id": 2, "region": "MH2", "category": "demo", "country": "India2", "descp": "due date for filing GSTR-7", "applicableto": "", "duedate": "2020-01-10", "previousdate": null } ], "2020-01-28": [ { "id": 3, "region": "GJ", "category": "test", "country": "India", "descp": "GSTR-11 Return for details of goods and services purchased in India", "applicableto": "", "duedate": "2020-01-28", "previousdate": null }, { "id": 3, "region": "MH", "category": "test", "country": "India", "descp": "GSTR-11 Return for details of goods and services purchased in India", "applicableto": "", "duedate": "2020-01-28", "previousdate": null } ] } const findByRegion = (json, regionArray) => { return Object.entries(json).reduce((acc, [periodKey, periodValue]) => { const filteredByRegion = periodValue.filter(item => regionArray.includes(item.region)) if (filteredByRegion.length) { return { ...acc, [periodKey]: filteredByRegion } } return acc }, {}) } console.log( findByRegion(myJson, ['MH', 'MH2']) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM