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

基于键映射计算足球比赛事件发生平均间隔分钟数

足球比赛事件平均间隔计算方案

原始数据

const gameEvents = new Map([
  [17, '⚽ GOAL'],
  [36, '🔁 Substitution'],
  [47, '⚽ GOAL'],
  [61, '🔁 Substitution'],
  [64, '🟡 Yellow card'],
  [69, '🔴 Red card'],
  [70, '🔁 Substitution'],
  [72, '🔁 Substitution'],
  [76, '⚽ GOAL'],
  [80, '⚽ GOAL'],
  [92, '🟡 Yellow card'],
]);

实现步骤与代码

  1. 提取事件分钟数
    通过扩展运算符将Map的键(事件发生分钟)转换为数组,方便后续计算:

    const eventMinutes = [...gameEvents.keys()];
    
  2. 计算平均间隔
    以比赛开始到最后一个事件的总时长除以事件总数,得到平均间隔并取整:

    const totalDuration = eventMinutes[eventMinutes.length - 1];
    const averageInterval = Math.round(totalDuration / eventMinutes.length);
    
  3. 输出指定格式结果
    使用模板字符串输出要求的内容:

    console.log(`an event happened on average every ${averageInterval} minutes`);
    

完整代码

const gameEvents = new Map([
  [17, '⚽ GOAL'],
  [36, '🔁 Substitution'],
  [47, '⚽ GOAL'],
  [61, '🔁 Substitution'],
  [64, '🟡 Yellow card'],
  [69, '🔴 Red card'],
  [70, '🔁 Substitution'],
  [72, '🔁 Substitution'],
  [76, '⚽ GOAL'],
  [80, '⚽ GOAL'],
  [92, '🟡 Yellow card'],
]);

const eventMinutes = [...gameEvents.keys()];
const totalDuration = eventMinutes[eventMinutes.length - 1];
const averageInterval = Math.round(totalDuration / eventMinutes.length);

console.log(`an event happened on average every ${averageInterval} minutes`);

补充说明

如果需要计算相邻事件之间的平均间隔,可替换计算逻辑为:

let totalGap = 0;
for (let i = 1; i < eventMinutes.length; i++) {
  totalGap += eventMinutes[i] - eventMinutes[i - 1];
}
const averageInterval = Math.round(totalGap / (eventMinutes.length - 1));

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 00:55:35