我有一些要按名称排序的对象数组。我正在使用lodash库并使用_.sortBy()。但是通过使用此功能,我没有得到正确的响应。
以下是我的activeUser数组
[ { date: 'Tue Dec 11 2018 00:00:00 GMT+0530 (IST)',
time: '10:30',
user_name: 'Happy Teachers DAy',
profilepic: 'http://192.168.0.109:8000//igXFkPIcNxSlhl0IB6c9iOexn.jpeg',
activeType: 'Group',
_id: '5b8f591bfd6e99021a2f1dd1',
isCreatedByTherapist: false,
groupMember: [] },
{ date: 'Tue Dec 11 2018 00:00:00 GMT+0530 (IST)',
time: '16:00',
user_name: 'Practice Group',
profilepic: null,
activeType: 'Group',
_id: '5bc1dce4b9d0faf0271e0ef5',
isCreatedByTherapist: false,
groupMember: [] },
{ date: 'Tue Dec 18 2018 00:00:00 GMT+0530 (IST)',
time: '16:00',
user_name: 'Practice Group',
profilepic: null,
activeType: 'Group',
_id: '5bc1dce4b9d0faf0271e0ef5',
isCreatedByTherapist: false,
groupMember: [] },
{ date: 'Sat Dec 15 2018 00:00:00 GMT+0530 (IST)',
time: '12:00',
user_name: 'Jayna',
profilepic: 'http://192.168.0.110:8000/D5sd646Tlx3zuOvWqhesSjIoL.jpeg',
activeType: 'Individual',
_id: '5b289024e019bb7623761bae',
isCreatedByTherapist: false,
groupMember: [] },
{ date: 'Fri Dec 12 2018 05:30:00 GMT+0530 (IST)',
time: '14:00',
user_name: 'GCK',
profilepic: 'http://192.168.0.110:8000/LDS3CpweCSSDDdOfa4lR6rPmn.jpeg',
activeType: 'Individual',
_id: '5ae1691241ce6c991f45b7fc',
isCreatedByTherapist: false,
groupMember: [] },
{ date: 'Fri Dec 11 2018 05:30:00 GMT+0530 (IST)',
time: '16:00',
user_name: 'kaushik',
profilepic: 'http://192.168.0.110:8000/DNAtiR1iiG2jgJWsIH5tpZCwv.jpeg',
activeType: 'Individual',
_id: '5b502b2724f36ada24167083',
isCreatedByTherapist: false,
groupMember: [] } ]
sortedName = _.sortBy(activeUser,['user_name']);
groupBy的代码如下:
var date = function (d) {
if (d.fullname != null || d.fullname != undefined) {
return d.fullname.toUpperCase().charAt(0);
}
}
// map a group to the required form
var groupToSummary = function (group, date) {
return {
date: date,
data: group
}
}
var data = _(sortedName)
.groupBy(date)
.map(groupToSummary)
.value();
在sortedName中,我得到以下数组作为响应:
[
{
"date": "G",
"data": [
{
"date": "Fri Dec 12 2018 05:30:00 GMT+0530 (IST)",
"time": "14:00",
"user_name": "GCK",
"profilepic": "http://192.168.0.110:8000/LDS3CpweCSSDDdOfa4lR6rPmn.jpeg",
"activeType": "Individual",
"_id": "5ae1691241ce6c991f45b7fc",
"isCreatedByTherapist": false,
"groupMember": []
}
]
},
{
"date": "H",
"data": [
{
"date": "Mon Dec 10 2018 00:00:00 GMT+0530 (IST)",
"time": "18:00",
"user_name": "Happy Teachers DAy",
"profilepic": "http://192.168.0.110:8000//igXFkPIcNxSlhl0IB6c9iOexn.jpeg",
"activeType": "Group",
"_id": "5b8f591bfd6e99021a2f1dd1",
"isCreatedByTherapist": false,
"groupMember": [
{
"groupMemberId": "5b46f08262efab2027ecce7a",
"_id": "5b90a8385ef8155c17865afc",
"isMultiSession": false
},
{
"groupMemberId": "5b502b2724f36ada24167083",
"_id": "5b90a8385ef8155c17865afb",
"isMultiSession": false
},
{
"groupMemberId": "5ae1691241ce6c991f45b7fc",
"_id": "5b90a8385ef8155c17865af9",
"isMultiSession": false
}
]
},
{
"date": "Tue Dec 11 2018 00:00:00 GMT+0530 (IST)",
"time": "10:30",
"user_name": "Happy Teachers DAy",
"profilepic": "http://192.168.0.110:8000//igXFkPIcNxSlhl0IB6c9iOexn.jpeg",
"activeType": "Group",
"_id": "5b8f591bfd6e99021a2f1dd1",
"isCreatedByTherapist": false,
"groupMember": [
{
"groupMemberId": "5b46f08262efab2027ecce7a",
"_id": "5b90a8385ef8155c17865afc",
"isMultiSession": false
},
{
"groupMemberId": "5b502b2724f36ada24167083",
"_id": "5b90a8385ef8155c17865afb",
"isMultiSession": false
},
{
"groupMemberId": "5ae1691241ce6c991f45b7fc",
"_id": "5b90a8385ef8155c17865af9",
"isMultiSession": false
}
]
}
]
},
{
"date": "J",
"data": [
{
"date": "Sat Dec 15 2018 00:00:00 GMT+0530 (IST)",
"time": "12:00",
"user_name": "Jayna",
"profilepic": "http://192.168.0.110:8000/D5sd646Tlx3zuOvWqhesSjIoL.jpeg",
"activeType": "Individual",
"_id": "5b289024e019bb7623761bae",
"isCreatedByTherapist": false,
"groupMember": []
},
{
"date": "Sat Dec 10 2018 05:30:00 GMT+0530 (IST)",
"time": "11:00",
"user_name": "Jayna",
"profilepic": "http://192.168.0.110:8000/D5sd646Tlx3zuOvWqhesSjIoL.jpeg",
"activeType": "Individual",
"_id": "5b289024e019bb7623761bae",
"isCreatedByTherapist": false,
"groupMember": []
}
]
},
{
"date": "P",
"data": [
{
"date": "Tue Dec 11 2018 00:00:00 GMT+0530 (IST)",
"time": "16:00",
"user_name": "Practice Group",
"profilepic": null,
"activeType": "Group",
"_id": "5bc1dce4b9d0faf0271e0ef5",
"isCreatedByTherapist": false,
"groupMember": []
}
]
},
{
"date": "K",
"data": [
{
"date": "Fri Dec 11 2018 05:30:00 GMT+0530 (IST)",
"time": "16:00",
"user_name": "kaushik",
"profilepic": "http://192.168.0.110:8000/DNAtiR1iiG2jgJWsIH5tpZCwv.jpeg",
"activeType": "Individual",
"_id": "5b502b2724f36ada24167083",
"isCreatedByTherapist": false,
"groupMember": []
}
]
}
]
如您所见,最后两个对象不合适!
请帮帮我!!!任何人的帮助将不胜感激!!!
答案 0 :(得分:0)
鉴于您显示的输入数据,我仍然无法获得所得到的结果,但是将大写的第一个字母称为“日期”似乎很奇怪,再加上您拥有引用full_name
的代码,但事实并非如此该值在数据中的属性。
小写字母k
使用_.sortBy
排在所有大写字母之后,因此这可能是问题的一部分,以及为什么“ K”组排在最后,但是给出了代码,数据和您显示的输出不是“可能”,我不确定我还能提供什么帮助。
var activeUser = [{
date: 'Tue Dec 11 2018 00:00:00 GMT+0530 (IST)',
time: '10:30',
user_name: 'Happy Teachers DAy',
profilepic: 'http://192.168.0.109:8000//igXFkPIcNxSlhl0IB6c9iOexn.jpeg',
activeType: 'Group',
_id: '5b8f591bfd6e99021a2f1dd1',
isCreatedByTherapist: false,
groupMember: []
},
{
date: 'Tue Dec 11 2018 00:00:00 GMT+0530 (IST)',
time: '16:00',
user_name: 'Practice Group',
profilepic: null,
activeType: 'Group',
_id: '5bc1dce4b9d0faf0271e0ef5',
isCreatedByTherapist: false,
groupMember: []
},
{
date: 'Tue Dec 18 2018 00:00:00 GMT+0530 (IST)',
time: '16:00',
user_name: 'Practice Group',
profilepic: null,
activeType: 'Group',
_id: '5bc1dce4b9d0faf0271e0ef5',
isCreatedByTherapist: false,
groupMember: []
},
{
date: 'Sat Dec 15 2018 00:00:00 GMT+0530 (IST)',
time: '12:00',
user_name: 'Jayna',
profilepic: 'http://192.168.0.110:8000/D5sd646Tlx3zuOvWqhesSjIoL.jpeg',
activeType: 'Individual',
_id: '5b289024e019bb7623761bae',
isCreatedByTherapist: false,
groupMember: []
},
{
date: 'Fri Dec 12 2018 05:30:00 GMT+0530 (IST)',
time: '14:00',
user_name: 'GCK',
profilepic: 'http://192.168.0.110:8000/LDS3CpweCSSDDdOfa4lR6rPmn.jpeg',
activeType: 'Individual',
_id: '5ae1691241ce6c991f45b7fc',
isCreatedByTherapist: false,
groupMember: []
},
{
date: 'Fri Dec 11 2018 05:30:00 GMT+0530 (IST)',
time: '16:00',
user_name: 'kaushik',
profilepic: 'http://192.168.0.110:8000/DNAtiR1iiG2jgJWsIH5tpZCwv.jpeg',
activeType: 'Individual',
_id: '5b502b2724f36ada24167083',
isCreatedByTherapist: false,
groupMember: []
}
];
let sortedArray = _
.chain(activeUser)
.sortBy(['user_name'])
.groupBy(function(d) {
if (d.fullname != null || d.fullname != undefined) {
return d.fullname.toUpperCase().charAt(0);
} else {
return d.user_name.toUpperCase().charAt(0);
}
})
.map(function(group, date) {
return ({
date: date,
data: group
});
})
.value();
console.dir(sortedArray);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>