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 badges1 Answer
Reset to default 5The 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();
};
版权声明:本文标题:javascript - Use _.chain .groupBy .filter .map in lodash to get grouping based on different filters - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744228921a2596231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论