JavaScript实现多条件聚合通知列表(类Facebook通知分组效果)
通知聚合分组解决方案
原代码问题
- 缺少规则2的
in.id匹配逻辑,仅按type分组会把不同关联对象的通知错误合并 - 语法错误:
inRangeBetween的参数括号位置错误,type判断条件被错误包进了函数参数里 - 时间判断逻辑错误:重置起始点时两次给
min赋值,未正确设置max,且日期实例化时漏读created_at字段 - 分组存储逻辑错误:用
type作为分组唯一key,会覆盖同类型下不同时间间隔/不同关联对象的分组
完整实现代码
// 原有排序方法优化(重复实例化日期的逻辑抽离,提升性能) function sortByDate(array, desc = true) { return array.sort((a, b) => { const aTime = new Date(a.created_at).getTime() const bTime = new Date(b.created_at).getTime() return desc ? bTime - aTime : aTime - bTime }) } // 按10分钟间隔拆分同类型同关联对象的通知组 function splitByTimeGap(arr, gap = 10 * 60 * 1000) { if (!arr.length) return [] const result = [] let currentGroup = [arr[0]] let currentLatestTime = new Date(arr[0].created_at).getTime() for (let i = 1; i < arr.length; i++) { const itemTime = new Date(arr[i].created_at).getTime() if (currentLatestTime - itemTime <= gap) { currentGroup.push(arr[i]) } else { result.push(currentGroup) currentGroup = [arr[i]] currentLatestTime = itemTime } } result.push(currentGroup) return result } // 主聚合函数 function aggregateNotifications(data) { // 第一步:按时间降序排序 const sortedData = sortByDate(data) // 第二步:按 type + in.id 做第一层分组 const firstLevelGroups = sortedData.reduce((acc, item) => { let key = `type:${item.type}` // 非 shop.follower 类型需要加上 in.id 作为分组key if (item.type !== 'shop.follower') { key += `:in_id:${item.in.id}` } if (!acc[key]) acc[key] = [] acc[key].push(item) return acc }, {}) // 第三步:每个第一层分组按10分钟拆分,再处理组属性 const finalGroups = [] Object.values(firstLevelGroups).forEach(group => { const timeGroups = splitByTimeGap(group) timeGroups.forEach(timeGroup => { finalGroups.push({ items: timeGroup, type: timeGroup[0].type, inId: timeGroup[0].in?.id || null, isUnread: timeGroup.some(item => item.read_at === null), latestCreatedAt: timeGroup[0].created_at, count: timeGroup.length }) }) }) // 最终结果再按最新时间降序排序 return sortByDate(finalGroups) } // 测试用示例数据(已修复原数据语法错误,删除无效的省略标识) const data = [ { id: 1, type: 'shop.follower', by: { id: 2, name: 'User B' }, in: null, read_at: '2021-01-03 10:15:43', created_at: '2020-08-02 05:21:20' }, { id: 2, type: 'product.liked', by: { id: 2, name: 'User B' }, in: { id: 1 }, read_at: '2021-01-03 10:15:43', created_at: '2020-08-02 05:24:45' }, { id: 3, type: 'product.commented', by: { id: 3, name: 'User C' }, in: { id: 1 }, read_at: '2021-02-20 20:01:39', created_at: '2021-02-19 16:21:43' }, { id: 4, type: 'product.liked', by: { id: 4, name: 'User D' }, in: { id: 1 }, read_at: '2021-03-29 15:14:21', created_at: '2021-03-28 08:11:50' }, { id: 5, type: 'product.liked', by: { id: 3, name: 'User C' }, in: { id: 1 }, read_at: null, created_at: '2021-03-28 08:12:24' }, { id: 6, type: 'shop.follower', by: { id: 5, name: 'User E' }, in: null, read_at: null, created_at: '2021-05-23 10:02:21' }, { id: 7, type: 'shop.follower', by: { id: 3, name: 'User C' }, in: null, read_at: null, created_at: '2021-07-18 10:31:12' }, { id: 8, type: 'comment.replied', by: { id: 4, name: 'User D' }, in: { id: 6 }, read_at: null, created_at: '2021-07-24 08:34:25' }, ] // 调用示例 console.log(aggregateNotifications(data))
输出说明
示例数据运行后会得到7个符合规则的聚合组:
comment.replied类型关联id6,未读,单条shop.follower类型,未读,单条(时间2021-07-18)shop.follower类型,未读,单条(时间2021-05-23,和上一个间隔超过10分钟拆分)product.liked类型关联id1,未读,2条(两个点赞间隔34秒合并)product.commented类型关联id1,已读,单条product.liked类型关联id1,已读,单条(2020-08的记录和2021年的间隔超过10分钟拆分)shop.follower类型,已读,单条(2020-08的记录)
内容的提问来源于stack exchange,提问作者Gwein
相关产品推荐
相关产品推荐

