【发布时间】:2019-04-25 11:01:21
【问题描述】:
我想根据“title”和“alertID”在JSON下面分组,然后按升序或降序排序。
let events = [{
"_id": "5cbdb030284dfeb0bb441bfe",
"alert": {
"title": "New Pt w/ discharge in last 30 days",
"alertTime": "2018-12-15T05:48:31.000Z",
"alertID": "99994444"
}
}, {
"_id": "5cbdafbc284dfeb0bb441bd7",
"alert": {
"title": "Insurance: Non-Contracted Plan",
"alertTime": "2018-09-01T03:13:00.000Z",
"alertID": "12345699"
}
}, {
"_id": "5cbdafbe284dfeb0bb441be6",
"alert": {
"title": "Insurance: Non-Contracted Plan",
"alertTime": "2018-09-01T03:13:00.000Z",
"alertID": "12345688"
}
}, {
"_id": "5cbdb08d284dfeb0bb441c13",
"alert": {
"title": "New Pt w/ ED visit in last 7 days",
"alertTime": "2018-12-15T05:48:31.000Z",
"alertID": "12345699"
}
}, {
"_id": "5cbdb068284dfeb0bb441c09",
"alert": {
"title": "New Pt w/ ED visit in last 7 days",
"alertTime": "2018-12-15T05:48:31.000Z",
"alertID": "12345688"
}
}, {
"_id": "5cbdb477284dfeb0bb441c73",
"alert": {
"title": "PT Consult Order",
"alertTime": "2018-07-17T05:42:15.000Z",
"alertID": "12345699"
}
}, {
"_id": "5cbdafb8284dfeb0bb441bc7",
"alert": {
"title": "Bed Request Order for Medicare Patient",
"alertTime": "2019-04-22T12:12:39.340Z",
"alertID": "99994444"
}
}]
这就是我试图实现(降序)它但它没有被排序的方式。
_(events)
.orderBy(events, (e) => { return e.alert.alertTime},['desc'])
.uniqBy(function(event) { return [event.alert.title, event.alert.alertID].join();})
.groupBy((event) => event.alert.alertID)
我特别想使用 lodash 来实现这个要求。有人可以指出我缺少什么吗?
这是我期望的输出。因此,如果“alertID:99994444”首先出现,那么我想对具有相同“alertID”的所有数据进行分组,并按升序或降序对它们进行排序,如下所示。同样,应该对所有剩余的“alertID”执行此操作。:
[{
"_id": "5cbdb477284dfeb0bb441c73",
"alert": {
"title": "PT Consult Order",
"alertTime": "2018-07-17T05:42:15.000Z",
"alertID": "12345699"
}
}, {
"_id": "5cbdafbc284dfeb0bb441bd7",
"alert": {
"title": "Insurance: Non-Contracted Plan",
"alertTime": "2018-09-01T03:13:00.000Z",
"alertID": "12345699"
}
}, {
"_id": "5cbdb08d284dfeb0bb441c13",
"alert": {
"title": "New Pt w/ ED visit in last 7 days",
"alertTime": "2018-12-15T05:48:31.000Z",
"alertID": "12345699"
}
}, {
"_id": "5cbdafbe284dfeb0bb441be6",
"alert": {
"title": "Insurance: Non-Contracted Plan",
"alertTime": "2018-09-01T03:13:00.000Z",
"alertID": "12345688"
}
}, {
"_id": "5cbdb068284dfeb0bb441c09",
"alert": {
"title": "New Pt w/ ED visit in last 7 days",
"alertTime": "2018-12-15T05:48:31.000Z",
"alertID": "12345688"
}
}, {
"_id": "5cbdb030284dfeb0bb441bfe",
"alert": {
"title": "New Pt w/ discharge in last 30 days",
"alertTime": "2018-12-15T05:48:31.000Z",
"alertID": "99994444"
}
}, {
"_id": "5cbdafb8284dfeb0bb441bc7",
"alert": {
"title": "Bed Request Order for Medicare Patient",
"alertTime": "2019-04-22T12:12:39.340Z",
"alertID": "99994444"
}
}]
【问题讨论】:
-
请发布预期输出
-
@adiga 我已经添加了所需的输出。
标签: javascript momentjs lodash