首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在javascript中按时间范围分组而不考虑日/月/年

如何在javascript中按时间范围分组而不考虑日/月/年
EN

Stack Overflow用户
提问于 2022-10-18 12:46:30
回答 2查看 47关注 0票数 0

嗨!

我需要使用timestamps(data)从输入列表中创建一个显示与其时间范围相关的分数的objs数组(结果),而不考虑日/月/年。

我认为要做到这一点,我首先需要创建一个由时间范围组织的objs子数组,然后映射所有的(groupByTimeRange),我不知道如何去做。

时间范围:

  • 8h包括6h至9h29
  • 11h包括9h30至12h29
  • 14h包括12h30至15h29
  • 17h包括15h30至18h29

H 11520h包括18h30至23h59H 216f 217

数据

代码语言:javascript
运行
复制
 const data = 
   [
    {id: 3337, score: 75, date: '2000-02-02T07:25:00.000Z'},
    {id: 3336, score: 30, date: '2012-06-02T08:24:00.000Z'},
    {id: 3335, score: 25, date: '2020-08-02T09:23:00.000Z'},
    {id: 2234, score: 85, date: '2018-08-02T12:20:00.000Z'},
    {id: 1534, score: 85, date: '2016-08-02T10:30:00.000Z'},
    {id: 3884, score: 85, date: '2019-08-02T11:18:00.000Z'},
    {id: 6534, score: 25, date: '2012-08-02T13:01:00.000Z'},
    {id: 3484, score: 14, date: '2022-08-02T15:22:00.000Z'},
    {id: 2224, score: 87, date: '2021-08-02T14:17:00.000Z'},
    {id: 2234, score: 59, date: '2018-08-02T17:20:00.000Z'},
    {id: 1534, score: 48, date: '2016-08-02T18:24:00.000Z'},
    {id: 3884, score: 25, date: '2019-08-02T16:18:00.000Z'},
    {id: 3287, score: 21, date: '2000-02-02T19:25:00.000Z'},
    {id: 3496, score: 05, date: '2012-06-02T20:24:00.000Z'},
    {id: 9635, score: 67, date: '2020-08-02T22:23:00.000Z'}
   ],

按时间范围分组8h/11h/14h/17h/20h

这部分我不知道怎么做。

代码语言:javascript
运行
复制
const groupByTimeRange = 
 [
   // Group Time range: 8h
   [
    {id: 3337, score: 75, date: '2000-02-02T07:25:00.000Z'},
    {id: 3336, score: 30, date: '2012-06-02T08:24:00.000Z'},
    {id: 3335, score: 25, date: '2020-08-02T09:23:00.000Z'}
   ],
   // Group Time range: 11h 
   [
    {id: 2234, score: 85, date: '2018-08-02T12:20:00.000Z'},
    {id: 1534, score: 85, date: '2016-08-02T10:30:00.000Z'},
    {id: 3884, score: 85, date: '2019-08-02T11:18:00.000Z'}
   ],
   // Group Time range: 14h 
   [
    {id: 6534, score: 25, date: '2012-08-02T13:01:00.000Z'},
    {id: 3484, score: 14, date: '2022-08-02T15:22:00.000Z'},
    {id: 2224, score: 87, date: '2021-08-02T14:17:00.000Z'}
   ],
   // Group Time range: 17h 
   [
    {id: 2234, score: 59, date: '2018-08-02T17:20:00.000Z'},
    {id: 1534, score: 48, date: '2016-08-02T18:24:00.000Z'},
    {id: 3884, score: 25, date: '2019-08-02T16:18:00.000Z'}
   ],
   // Group Time range: 20h 
   [
    {id: 3287, score: 21, date: '2000-02-02T19:25:00.000Z'},
    {id: 3496, score: 05, date: '2012-06-02T20:24:00.000Z'},
    {id: 9635, score: 67, date: '2020-08-02T22:23:00.000Z'}
   ],
  ]

预期产出

代码语言:javascript
运行
复制
const result = 
  [
    {timeRange:"8h", AverageScore: 43%},
    {timeRange:"11h", AverageScore: 85%},
    {timeRange:"14h", AverageScore: 42%},
    {timeRange:"17h", AverageScore: 44%},
    {timeRange:"20h", AverageScore: 31%}
  ]

谢谢你花时间帮我。

如果有什么事情不清楚,请在评论中告诉我,我会修改的。

EN

回答 2

Stack Overflow用户

发布于 2022-10-18 12:55:19

这是我的版本

代码语言:javascript
运行
复制
const ranges = [
{ name: "0h", start: "00:00", end: "06:29" },
{ name: "8h", start: "06:00", end: "09:29" },
{ name:"11h", start: "09:30", end: "12:29" },
{ name:"14h", start: "12:30", end: "15:29" },
{ name:"17h", start: "15:30", end: "18:29" },
{ name:"20h", start: "18:30", end: "23:29" }];

const groupByTimeRange = data.reduce((acc,cur) => {
  const time = cur.date.split("T")[1].slice(0,-8); // many ways to skin a cat
  const range = ranges.find(range => time >= range.start && time <= range.end); // find returns the first found
  const name = range.name;
  (acc[name] = acc[name] || []).push(cur); // (if there, reuse, otherwise create) then push
  return acc;
},{})

console.log(groupByTimeRange)

const results = Object.entries(groupByTimeRange).map(([key,val]) => {
  const sum =  val.reduce((a,b) => { return a+b.score },0);
  const avg = Math.round(sum/val.length);
  return  {"timeRange": key, "AverageScore": `${avg}%` };
});

console.log(results);
代码语言:javascript
运行
复制
<script>
const data =    [
    {id: 3337, score: 75, date: '2000-02-02T07:25:00.000Z'},
    {id: 3336, score: 30, date: '2012-06-02T08:24:00.000Z'},
    {id: 3335, score: 25, date: '2020-08-02T09:23:00.000Z'},
    {id: 2234, score: 85, date: '2018-08-02T12:20:00.000Z'},
    {id: 1534, score: 85, date: '2016-08-02T10:30:00.000Z'},
    {id: 3884, score: 85, date: '2019-08-02T11:18:00.000Z'},
    {id: 6534, score: 25, date: '2012-08-02T13:01:00.000Z'},
    {id: 3484, score: 14, date: '2022-08-02T15:22:00.000Z'},
    {id: 2224, score: 87, date: '2021-08-02T14:17:00.000Z'},
    {id: 2234, score: 59, date: '2018-08-02T17:20:00.000Z'},
    {id: 1534, score: 48, date: '2016-08-02T18:24:00.000Z'},
    {id: 3884, score: 25, date: '2019-08-02T16:18:00.000Z'},
    {id: 3287, score: 21, date: '2000-02-02T19:25:00.000Z'},
    {id: 3496, score: 05, date: '2012-06-02T20:24:00.000Z'},
    {id: 9635, score: 67, date: '2020-08-02T22:23:00.000Z'}
   ]
</script>

票数 1
EN

Stack Overflow用户

发布于 2022-10-18 13:00:40

一个简单的解决方案是将范围存储在三元组的name - min - max中。

代码语言:javascript
运行
复制
const ranges = [
    ['8h', '06:00', '09:29'],
    ['11h', '09:30', '12:29'],
    ['14h', '12:30', '15:29'],
    ['17h', '15:30', '18:29'],
    ['20h', '18:30', '23:59'],
]

并遍历查找日期所属位置的范围:

代码语言:javascript
运行
复制
function getRange(date) {
    let hm = date.slice(11, 16)
    for (let [name, min, max] of ranges) {
        if (min <= hm && hm <= max)
            return name;
    }
}

一旦工作成功,在主数组上循环并填充分组映射:

代码语言:javascript
运行
复制
let groups = new Map()

for (let d of data) {
    let rng = getRange(d.date)
    if (!groups.has(rng))
        groups.set(rng, [])
    groups.get(rng).push(d)
}

此地图的.values()将是您的groupByTimeRange数组。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74111228

复制
相关文章

相似问题

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