嗨
我需要从带有时间戳的输入列表(数据(中创建一个对象数组,显示与其时间范围相关的分数(result。
我认为要做到这一点,我需要首先创建一个按时间范围组织的obj子阵列阵列,然后映射所有对象(groupByTimeRange(,我不知道如何做到这一步。
时间范围:
- 8h包括6h至9h29
- 11h包括9h30至12h29
- 14h包括12h30至15h29
- 17h包括15h30至18h29
- 20h包括18h30至23h59
数据
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%}
]
谢谢你花时间帮助我。
如果有什么不清楚的地方,请在评论中告诉我,我会更改它
一个简单的解决方案是将范围存储在三元组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
数组。
这是我的版本
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);
<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>