title: Use _.chain .groupBy .filter .map in lodash to get grouping based on different filters
body: <p>Data Set:</p>
  
  <pre><code>[
    {
      "created_by": "5750790484393984",
      "patient_dateofbirth": "02/05/1990",
      "patient_aadhaar": "1234567890",
      "patient_area": "wert",
      "patient_state": "Arunachal Pradesh",
      "patient_gender": "Male",
      "patient_pincode": "123456",
      "location": "Guwahati",
      "patient_name": "Rakesh",
      "isVisited": 2,
      "new_repeat": "Repeat",
      "lastVisitDate": "2017-07-04",
      "updatedAt": "2017-07-04T04:41:59.261Z",
      "CreatedAt": "2017-07-03T18:48:25.773Z",
      "patient_bloodgroup": "B+",
      "patient_country": "India"
    },
    {
      "created_by": "5750790484393984",
      "patient_dateofbirth": "07/30/2006",
      "patient_aadhaar": "2343524361731",
      "patient_area": "begusarai",
      "patient_state": "Bihar",
      "patient_gender": "Male",
      "patient_pincode": "110016",
      "patient_name": "gyguy yuguy",
      "registration_number": "2",
      "ip_address": "43.225.248.20, 162.158.155.110",
      "CreatedAt": "2017-06-28T09:41:47.188Z",
      "patient_bloodgroup": "A+",
      "patient_country": "India"
    },
    {
      "ip_address": "103.83.148.161, 141.101.99.35",
      "lastVisitDate": "2017-07-04",
      "updatedAt": "2017-07-04T09:56:51.588Z",
      "CreatedAt": "2017-07-02T23:37:29.512Z",
      "patient_bloodgroup": "A+",
      "patient_country": "India",
      "created_by": "5750790484393984",
      "patient_dateofbirth": "06/24/1964",
      "patient_aadhaar": "",
      "patient_area": "Chattarpur",
      "patient_state": "Uttar Pradesh",
      "patient_gender": "Female",
      "patient_pincode": "201005",
      "patient_name": "Sudha Ambastha",
      "isVisited": 2,
      "new_repeat": "Repeat"
    },
    {
      "patient_area": "ajsdh",
      "patient_state": "Delhi",
      "patient_gender": "Male",
      "patient_pincode": "110074",
      "patient_name": "prashant",
      "registration_number": "283423472834",
      "isVisited": 1,
      "new_repeat": "New",
      "ip_address": "43.225.248.20, 162.158.155.110",
      "lastVisitDate": "2017-07-04",
      "updatedAt": "2017-07-03T18:41:55.777Z",
      "CreatedAt": "2017-06-28T11:18:55.792Z",
      "patient_bloodgroup": "B+",
      "patient_country": "India",
      "created_by": "5750790484393984",
      "patient_dateofbirth": "06/13/1990",
      "patient_aadhaar": "2131231238"
    },
    {
      "created_by": "5750790484393984",
      "patient_dateofbirth": "06/07/1990",
      "patient_aadhaar": "1278292020029",
      "patient_area": "Bhubneshwar",
      "patient_state": "Orissa",
      "patient_gender": "Male",
      "patient_pincode": "112218",
      "location": "Haldia",
      "patient_name": "Rajesh Mishra",
      "isVisited": 2,
      "new_repeat": "Repeat",
      "lastVisitDate": "2017-07-04",
      "updatedAt": "2017-07-04T04:40:26.724Z",
      "CreatedAt": "2017-07-03T04:21:35.647Z",
      "patient_bloodgroup": "B-",
      "patient_country": "India"
    }
  ]
  </code></pre>
  
  <p>I want to group the above data based on CreatedAt column and filter data on the basis of <strong><em>new_repeat</em></strong> column in the data set. I have already written a function for this, which is using _.chain and grouping the data but I am not able to filter it. Check the function below:</p>
  
  <pre><code>const formatDataCount = (data, group_by) =&gt; {
    return  _.chain(data)
      .sortBy('CreatedAt')
      .groupBy(datum =&gt; moment(datum.CreatedAt).format(group_by) )
      .map((value, key) =&gt; {
        return {
          label: key, 
          value:  _.size(value)
        }
      })
      .value();
  };
  </code></pre>
  
  <p>group_by is the date format and can be "DD-MM-YYYY" or "MMM"
  So, above is the function.How do filter parameter and value to it.</p>
  
  <p>Please suggest changes:</p>
  
  <pre><code>const formatDataCount = (data, group_by) =&gt; {
    return  _.chain(data)
      .sortBy('CreatedAt')
      .groupBy(datum =&gt; moment(datum.CreatedAt).format(group_by) )
      .filter(filter_by =&gt; filter_by.new_repeat == "New")
      .map((value, key) =&gt; {
        return {
          label: key, 
          value:  _.size(value)
        }
      })
      .value();
  };
  </code></pre>
  
  <p>First one is working, 2nd one isn't.</p>