首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对特定类型的数据进行分组,并使用存档按升序或降序排序。

对特定类型的数据进行分组,并使用存档按升序或降序排序。
EN

Stack Overflow用户
提问于 2019-04-25 11:01:21
回答 1查看 44关注 0票数 0

我想根据“标题”和"alertID“在JSON下面分组,然后按升序或降序排序。

代码语言:javascript
复制
    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"
    }
}]

这就是我如何实现(降序),但它没有得到排序。

代码语言:javascript
复制
_(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)

我特别想利用房客来达到这一要求。有人能指出我错过了什么吗?

--这是我所期望的输出。因此,如果"alertID: 99994444“首先出现,那么我想对所有具有相同'alertID‘的数据进行分组,并按升序或降序排序,如下所示。同样,对于所有剩余的“警报be”,都应该这样做。

代码语言:javascript
复制
[{
    "_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"
    }
}]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-25 11:14:34

问题是,在使用_(events)链接时,应该省略链式方法调用中的第一个“集合”参数。因此,改变:

代码语言:javascript
复制
_(events)
        .orderBy(events, (e) => { return e.alert.alertTime},['desc'])

至:

代码语言:javascript
复制
_(events)
        .orderBy((e) => { return e.alert.alertTime},['desc'])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55847771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档