1

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
      }
    ]
}
1
  • this question has nothing to do with vue, so I removed the tag. Commented Jan 22, 2020 at 10:41

2 Answers 2

2

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>

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

1 Comment

thanks buddy, but what if I have two arrays, regionArr = ['MH','MH2'] and categoryArr = ['demo'] now how do i filter the objects based on both the array.
1

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'])
)

3 Comments

OP has an array of regions to check - regionArr, not a single value.
I am new to js. I have edited my question. Do check.
@sheetaldharerao got you buddy, edited the answer, please check

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.