问:js如何对以下数据快速有效分组

江湖人称007
  • 370
[
  {
      "remark": "1",
      "createdTime": "2021-11-08 17:57:45",
      "timeLineType": 25,
      "timeLineTypeName": "2",
      "userName": "名字",
      "showRecord": 0,
      "talkerDetailUrl": "",
      "customerName": "你好",
      "keyword": "",
      "sensitiveWord": "",
      "numContent": 1,
      "transferNum": 0,
      "transRecvNum": 0,
      "transSendNum": 0,
      "transferAmount": "0.00",
      "types": "10002",
      "receive": 1,
      "send": 0,
      "visitingCard": 0,
      "imagesNum": 0,
      "fileNum": 0,
      "sendLocationNum": 0,
      "videoCallsNum": 0,
      "voiceCallsNum": 0,
      "linkNum": 0,
      "redPacketsNum": 0,
      "redSendNum": 0,
      "redRecvNum": 0,
      "sharedLocationNum": 0,
      "videoNum": 0,
      "voiceNum": 0,
      "startTime": 1636365465686,
      "endTime": 1636365465686,
      "createTimeL": 1636365465686
    },
    {
      "remark": "1",
      "createdTime": "2021-11-08 19:57:45",
      "timeLineType": 25,
      "timeLineTypeName": "2",
      "userName": "名字",
      "showRecord": 0,
      "talkerDetailUrl": "",
      "customerName": "你好",
      "keyword": "",
      "sensitiveWord": "",
      "numContent": 1,
      "transferNum": 0,
      "transRecvNum": 0,
      "transSendNum": 0,
      "transferAmount": "0.00",
      "types": "10002",
      "receive": 1,
      "send": 0,
      "visitingCard": 0,
      "imagesNum": 0,
      "fileNum": 0,
      "sendLocationNum": 0,
      "videoCallsNum": 0,
      "voiceCallsNum": 0,
      "linkNum": 0,
      "redPacketsNum": 0,
      "redSendNum": 0,
      "redRecvNum": 0,
      "sharedLocationNum": 0,
      "videoNum": 0,
      "voiceNum": 0,
      "startTime": 1636365465686,
      "endTime": 1636365465686,
      "createTimeL": 1636365465686
    },
    {
      "remark": "1",
      "createdTime": "2021-11-09 17:57:45",
      "timeLineType": 25,
      "timeLineTypeName": "2",
      "userName": "名字",
      "showRecord": 0,
      "talkerDetailUrl": "",
      "customerName": "你好",
      "keyword": "",
      "sensitiveWord": "",
      "numContent": 1,
      "transferNum": 0,
      "transRecvNum": 0,
      "transSendNum": 0,
      "transferAmount": "0.00",
      "types": "10002",
      "receive": 1,
      "send": 0,
      "visitingCard": 0,
      "imagesNum": 0,
      "fileNum": 0,
      "sendLocationNum": 0,
      "videoCallsNum": 0,
      "voiceCallsNum": 0,
      "linkNum": 0,
      "redPacketsNum": 0,
      "redSendNum": 0,
      "redRecvNum": 0,
      "sharedLocationNum": 0,
      "videoNum": 0,
      "voiceNum": 0,
      "startTime": 1636365465686,
      "endTime": 1636365465686,
      "createTimeL": 1636365465686
    },
    {
      "remark": "1",
      "createdTime": "2021-11-09 17:57:45",
      "timeLineType": 25,
      "timeLineTypeName": "2",
      "userName": "名字",
      "showRecord": 0,
      "talkerDetailUrl": "",
      "customerName": "你好",
      "keyword": "",
      "sensitiveWord": "",
      "numContent": 1,
      "transferNum": 0,
      "transRecvNum": 0,
      "transSendNum": 0,
      "transferAmount": "0.00",
      "types": "10002",
      "receive": 1,
      "send": 0,
      "visitingCard": 0,
      "imagesNum": 0,
      "fileNum": 0,
      "sendLocationNum": 0,
      "videoCallsNum": 0,
      "voiceCallsNum": 0,
      "linkNum": 0,
      "redPacketsNum": 0,
      "redSendNum": 0,
      "redRecvNum": 0,
      "sharedLocationNum": 0,
      "videoNum": 0,
      "voiceNum": 0,
      "startTime": 1636365465686,
      "endTime": 1636365465686,
      "createTimeL": 1636365465686
    },
    {
      "remark": "1",
      "createdTime": "2021-11-10 17:57:45",
      "timeLineType": 25,
      "timeLineTypeName": "2",
      "userName": "名字",
      "showRecord": 0,
      "talkerDetailUrl": "",
      "customerName": "你好",
      "keyword": "",
      "sensitiveWord": "",
      "numContent": 1,
      "transferNum": 0,
      "transRecvNum": 0,
      "transSendNum": 0,
      "transferAmount": "0.00",
      "types": "10002",
      "receive": 1,
      "send": 0,
      "visitingCard": 0,
      "imagesNum": 0,
      "fileNum": 0,
      "sendLocationNum": 0,
      "videoCallsNum": 0,
      "voiceCallsNum": 0,
      "linkNum": 0,
      "redPacketsNum": 0,
      "redSendNum": 0,
      "redRecvNum": 0,
      "sharedLocationNum": 0,
      "videoNum": 0,
      "voiceNum": 0,
      "startTime": 1636365465686,
      "endTime": 1636365465686,
      "createTimeL": 1636365465686
    },
    {
      "remark": "1",
      "createdTime": "2021-11-10 17:57:45",
      "timeLineType": 25,
      "timeLineTypeName": "2",
      "userName": "名字",
      "showRecord": 0,
      "talkerDetailUrl": "",
      "customerName": "你好",
      "keyword": "",
      "sensitiveWord": "",
      "numContent": 1,
      "transferNum": 0,
      "transRecvNum": 0,
      "transSendNum": 0,
      "transferAmount": "0.00",
      "types": "10002",
      "receive": 1,
      "send": 0,
      "visitingCard": 0,
      "imagesNum": 0,
      "fileNum": 0,
      "sendLocationNum": 0,
      "videoCallsNum": 0,
      "voiceCallsNum": 0,
      "linkNum": 0,
      "redPacketsNum": 0,
      "redSendNum": 0,
      "redRecvNum": 0,
      "sharedLocationNum": 0,
      "videoNum": 0,
      "voiceNum": 0,
      "startTime": 1636365465686,
      "endTime": 1636365465686,
      "createTimeL": 1636365465686
    },
    {
      "remark": "1",
      "createdTime": "2021-11-11 17:57:45",
      "timeLineType": 25,
      "timeLineTypeName": "2",
      "userName": "名字",
      "showRecord": 0,
      "talkerDetailUrl": "",
      "customerName": "你好",
      "keyword": "",
      "sensitiveWord": "",
      "numContent": 1,
      "transferNum": 0,
      "transRecvNum": 0,
      "transSendNum": 0,
      "transferAmount": "0.00",
      "types": "10002",
      "receive": 1,
      "send": 0,
      "visitingCard": 0,
      "imagesNum": 0,
      "fileNum": 0,
      "sendLocationNum": 0,
      "videoCallsNum": 0,
      "voiceCallsNum": 0,
      "linkNum": 0,
      "redPacketsNum": 0,
      "redSendNum": 0,
      "redRecvNum": 0,
      "sharedLocationNum": 0,
      "videoNum": 0,
      "voiceNum": 0,
      "startTime": 1636365465686,
      "endTime": 1636365465686,
      "createTimeL": 1636365465686
    }
]

数据结构是这样,由于数据量大,简单的循环会明显感觉到卡顿

最终数据格式

[2021-11-08:[...],2021-11-09:[...],]
回复
阅读 1.2k
3 个回答
Kapok
  • 270
const resut = {}
data.forEach(item => {
    const key = item.createdTime.split(" ")[0]
    if(!resut[key]) resut[key] = []
    resut[key].push(item) 
})

直接循环 O(1) 理论上来说没有更快的了

这不光是排序,还得分组。如果只是排序,Array.prototype.sort 用的快速排序,应该不能再快了。但还要分组,用 Lodash 懒模式试试看。

import _ from "lodash";

const data = [ ... ];

const result = _(data)
    .groupBy(it => it.createdTime.substring(0, 10))
    .entries()
    .sortBy(it => it[0])
    .value();

这里得到的的结果是 [["2021-11-08", [...]], ["2021-11-09", [...]], ... ] 这样的。
如果要有序,只能是这样。题主那样的输出不存在,数组中不能用键值对;而对象可以用键值对,但不能保存顺序(虽然一般结果都是有序的)

如果需要对象,可以以 .value() 之前加 .fromPairs()
如果本来原数据就是有序的,在 .groupBy() 之后就直接 .value() 可能就能得到正确的对象结果(也就是只做分组)。

list.reduce((acc, cur) => ((acc[cur.createdTime.slice(0, 10)] ||= []).push(cur), acc), {})

楼上也有转换的程序。

页面卡顿的话可以放到后台运行的 Web Worker 中运行,相当于异步接口
http://www.ruanyifeng.com/blo...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏