嗨!
我需要使用timestamps(data)从输入列表中创建一个显示与其时间范围相关的分数的objs数组(结果),而不考虑日/月/年。
我认为要做到这一点,我首先需要创建一个由时间范围组织的objs子数组,然后映射所有的(groupByTimeRange),我不知道如何去做。
时间范围:
H 11520h包括18h30至23h59H 216f 217
数据
 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
这部分我不知道怎么做。
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'}
   ],
  ]预期产出
const result = 
  [
    {timeRange:"8h", AverageScore: 43%},
    {timeRange:"11h", AverageScore: 85%},
    {timeRange:"14h", AverageScore: 42%},
    {timeRange:"17h", AverageScore: 44%},
    {timeRange:"20h", AverageScore: 31%}
  ]谢谢你花时间帮我。
如果有什么事情不清楚,请在评论中告诉我,我会修改的。
发布于 2022-10-18 13:00:40
一个简单的解决方案是将范围存储在三元组的name - min - max中。
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'],
]并遍历查找日期所属位置的范围:
function getRange(date) {
    let hm = date.slice(11, 16)
    for (let [name, min, max] of ranges) {
        if (min <= hm && hm <= max)
            return name;
    }
}一旦工作成功,在主数组上循环并填充分组映射:
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数组。
https://stackoverflow.com/questions/74111228
复制相似问题