admin管理员组

文章数量:1401603

Data Set:

[
  {
    "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"
  }
]

I want to group the above data based on CreatedAt column and filter data on the basis of new_repeat 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:

const formatDataCount = (data, group_by) => {
  return  _.chain(data)
    .sortBy('CreatedAt')
    .groupBy(datum => moment(datum.CreatedAt).format(group_by) )
    .map((value, key) => {
      return {
        label: key, 
        value:  _.size(value)
      }
    })
    .value();
};

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.

Please suggest changes:

const formatDataCount = (data, group_by) => {
  return  _.chain(data)
    .sortBy('CreatedAt')
    .groupBy(datum => moment(datum.CreatedAt).format(group_by) )
    .filter(filter_by => filter_by.new_repeat == "New")
    .map((value, key) => {
      return {
        label: key, 
        value:  _.size(value)
      }
    })
    .value();
};

First one is working, 2nd one isn't.

Data Set:

[
  {
    "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"
  }
]

I want to group the above data based on CreatedAt column and filter data on the basis of new_repeat 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:

const formatDataCount = (data, group_by) => {
  return  _.chain(data)
    .sortBy('CreatedAt')
    .groupBy(datum => moment(datum.CreatedAt).format(group_by) )
    .map((value, key) => {
      return {
        label: key, 
        value:  _.size(value)
      }
    })
    .value();
};

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.

Please suggest changes:

const formatDataCount = (data, group_by) => {
  return  _.chain(data)
    .sortBy('CreatedAt')
    .groupBy(datum => moment(datum.CreatedAt).format(group_by) )
    .filter(filter_by => filter_by.new_repeat == "New")
    .map((value, key) => {
      return {
        label: key, 
        value:  _.size(value)
      }
    })
    .value();
};

First one is working, 2nd one isn't.

Share Improve this question asked Jul 4, 2017 at 12:35 prashantsudeepprashantsudeep 331 gold badge1 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

The filter line - .filter(filter_by => filter_by.new_repeat == "New") should be before the groupBy. I would also move it before the sortBy, because you don't need to sort data that you are removing.

const formatDataCount = (data, group_by) => {
  return  _.chain(data)
    .filter(filter_by => filter_by.new_repeat == "New")
    .sortBy('CreatedAt')
    .groupBy(datum => moment(datum.CreatedAt).format(group_by) )
    .map((value, key) => {
      return {
        label: key, 
        value:  _.size(value)
      }
    })
    .value();
};

本文标签: javascriptUse chain groupBy filter map in lodash to get grouping based on different filtersStack Overflow