gpt4 book ai didi

javascript - 在 lodash 中使用 _.chain .groupBy .filter .map 根据不同的过滤器进行分组

转载 作者:行者123 更新时间:2023-12-02 03:52:45 24 4
gpt4 key购买 nike

数据集:

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

我想根据 CreatedAt 列对上述数据进行分组,并根据数据集中的 new_repeat 列过滤数据。我已经为此编写了一个函数,它使用 _.chain 并对数据进行分组,但我无法对其进行过滤。检查以下功能:

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 是日期格式,可以是“DD-MM-YYYY”或“MMM”所以,上面是函数。如何过滤参数和值。

请提出修改建议:

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();
};

第一个有效,第二个无效。

最佳答案

过滤器行 - .filter(filter_by => filter_by.new_repeat == "New") 应该在 groupBy 之前。我还会将它移到 sortBy 之前,因为您不需要对要删除的数据进行排序。

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 - 在 lodash 中使用 _.chain .groupBy .filter .map 根据不同的过滤器进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44906175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com