gpt4 book ai didi

javascript - 使用 lodash 对特定类型的数据进行分组并按升序或降序对它们进行排序

转载 作者:行者123 更新时间:2023-12-02 23:46:17 27 4
gpt4 key购买 nike

我想根据“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"
}
}]

最佳答案

问题是,当您使用 _(events) 进行链接时,您应该省略链接方法调用中的第一个“集合”参数。所以改变:

_(events)
.orderBy(events, (e) => { return e.alert.alertTime},['desc'])

至:

_(events)
.orderBy((e) => { return e.alert.alertTime},['desc'])

关于javascript - 使用 lodash 对特定类型的数据进行分组并按升序或降序对它们进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55847771/

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