xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js group objects in an array

js group objects in an array

js group objects in an array

var groupBy = function(xs, key) {
    return xs.reduce(function(rv, x) {
      (rv[x[key]] = rv[x[key]] || []).push(x);
      return rv;
    }, {});
};


https://www.consolelog.io/group-by-in-javascript/

https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects

https://codereview.stackexchange.com/questions/37028/grouping-elements-in-array-by-multiple-properties



https://atendesigngroup.com/blog/array-map-filter-and-reduce-js



solution ??? sort & reduce

https://stackoverflow.com/questions/57355613/javascript-group-on-an-array-of-objects-with-timestamp-range-difference#



https://repl.it/@xgqfrms/grouping-objects-in-array-by-timestamp


/**
 * @authr xgqfrms
 * @ description grouping-objects-in-array-by-timestamp
 */

let log = console.log;

const datas = [
    { msgId: 606896983568064500, text: "B", time: "2019/08/02 17:11", isSelf: true },
    { msgId: 606897486704189400, text: "A", time: "2019/08/02 17:13", isSelf: true },
    { msgId: 606892034444533700, text: "C", time: "2019/08/02 16:52", isSelf: false },
    { msgId: 606889698041045000, text: "D", time: "2019/08/02 16:42", isSelf: false },
    { msgId: 606866947376980000, text: "E", time: "2019/08/02 15:12", isSelf: false },
    { msgId: 606866947376970000, text: "G", time: "2019/08/01 5:12", isSelf: false },
    { msgId: 606866947376910000, text: "F", time: "2019/08/01 15:12", isSelf: false },
];

log(`src datas =`, JSON.stringify(datas, null, 4));

// m. s, ms
const fiveMinutes = 5 * 60 * 1000;

let result = datas
    // timestamp
    // asc
    .sort((a, b) => new Date(a.time) - new Date(b.time))
    .reduce((arr, obj, i, {[i - 1]: last}) => {
        if (!last || new Date(obj.time) - new Date(last.time) > fiveMinutes) {
            arr.push([obj]);
        } else {
            arr[arr.length - 1].push(obj);
        }
        return arr;
    }, []);

log(`result =`, JSON.stringify(result, null, 4));

OK

[
    [
        {
            "msgId": 606866947376970000,
            "text": "G",
            "time": "2019/08/01 5:12",
            "isSelf": false
        }
    ],
    [
        {
            "msgId": 606866947376910000,
            "text": "F",
            "time": "2019/08/01 15:12",
            "isSelf": false
        }
    ],
    [
        {
            "msgId": 606866947376980000,
            "text": "E",
            "time": "2019/08/02 15:12",
            "isSelf": false
        }
    ],
    [
        {
            "msgId": 606889698041045000,
            "text": "D",
            "time": "2019/08/02 16:42",
            "isSelf": false
        }
    ],
    [
        {
            "msgId": 606892034444533800,
            "text": "C",
            "time": "2019/08/02 16:52",
            "isSelf": false
        }
    ],
    [
        {
            "msgId": 606896983568064500,
            "text": "B",
            "time": "2019/08/02 17:11",
            "isSelf": true
        },
        {
            "msgId": 606897486704189400,
            "text": "A",
            "time": "2019/08/02 17:13",
            "isSelf": true
        }
    ]
]

bug

https://repl.it/@xgqfrms/timestamp-group-bug







©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


posted @   xgqfrms  阅读(175)  评论(13编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2016-08-05 GitHub Ribbons : 谈网站的安全性-资源链接如何 预防/实现 爬虫的批量下载!
点击右上角即可分享
微信分享提示