【问题标题】:Grouping specific type of data and sorting them in ascending or descending order using lodash使用 lodash 对特定类型的数据进行分组并按升序或降序对它们进行排序
【发布时间】: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


【解决方案1】:

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

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

到:

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

【讨论】:

  • 我试过这个,但仍然面临同样的问题。我还在问题中添加了所需的输出。
猜你喜欢
  • 1970-01-01
  • 2015-08-30
  • 2020-08-09
  • 1970-01-01
  • 2010-09-17
  • 2021-04-17
  • 2014-02-16
  • 2021-08-21
  • 1970-01-01
相关资源
最近更新 更多