You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按ID统计指定时间段内的用户数量并对用户数据进行分组

按ID统计指定时间范围用户数量实现方案

前提条件

  • 起始时间戳:1512709024000
  • 时间跨度:10分钟,对应毫秒值为 10 * 60 * 1000 = 600000
  • 计算得有效时间范围的结束时间戳:1512709024000 + 600000 = 1512709624000

实现逻辑

  1. 遍历用户对象数组,筛选出时间戳在[1512709024000, 1512709624000]区间内的有效用户
  2. 以用户ID为Key,对有效用户进行分组,同时统计每个ID对应的用户数量

参考代码(JavaScript示例)

// 原始用户数据
const userList = [
  {
    "name": "user1",
    "id": "1",
    "timestamp": 1512754583000
  },
  {
    "name": "user2",
    "id": "1",
    "timestamp": 1512754631000
  },
  {
    "name": "user3",
    "id": "2",
    "timestamp": 1512709065294
  },
  {
    "name": "user4",
    "id": "2",
    "timestamp": 1512711000000
  },
  {
    "name": "user4",
    "id": "1",
    "timestamp": 1512754436000
  }
];

const startTime = 1512709024000;
const endTime = startTime + 10 * 60 * 1000;
const groupResult = {};

userList.forEach(user => {
  // 校验是否符合时间要求
  if (user.timestamp >= startTime && user.timestamp <= endTime) {
    // 按ID分组统计
    if (groupResult[user.id]) {
      groupResult[user.id].count++;
      groupResult[user.id].userList.push(user);
    } else {
      groupResult[user.id] = {
        count: 1,
        userList: [user]
      }
    }
  }
});

console.log(groupResult);

运行结果

符合时间条件的用户共1条,分组统计结果如下:

{
  "2": {
    "count": 1,
    "userList": [
      {
        "name": "user3",
        "id": "2",
        "timestamp": 1512709065294
      }
    ]
  }
}

内容的提问来源于stack exchange,提问作者ronoc4

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 15:39:03