格式化 javascript 函数以将匹配映射到时间组

Posted

技术标签:

【中文标题】格式化 javascript 函数以将匹配映射到时间组【英文标题】:Formatting a javascript function to map matches into timegroups 【发布时间】:2020-07-23 14:11:00 【问题描述】:

我一直在研究以下功能,该功能应该将一组比赛分类到时间段中,并确保一支球队不能同时进行两次比赛。但是,在运行时,我遇到了团队列表被清空然后程序崩溃的问题。我点击运行之前组件的状态是这样的 我已将我的代码放在下面并对其进行了评论,因此希望它有意义。

 assignTime()
        let matches=this.state.matches;
        console.log(matches);

        let table = [];
        let timeslots = this.state.timeLabels;
        console.log(timeslots);
        let pitches = this.state.pitches;
        console.log(pitches);

        for (let slot =0; slot<timeslots.length; slot++) //this code block runs through every time slot
            let slotMatches=[];
            let slotTeams= [];
            let skippedMatches =[];
            for (let pitchNo = 0; pitchNo<pitches.length; pitchNo++)  //running through the available pitches
                let match = matches[0];//getting first match from the list
                    while (slotTeams.includes(match.teamA) || slotTeams.includes(match.teamB))  //if one of the teams is already scheduled for that slot
                        skippedMatches.push(match); //skip this match
                        let index = matches.indexOf(match);
                        matches.splice(index, 1); //remove that match temporarily from the list
                        match = matches[0]; //get the next match up
                    
                    slotMatches.push(match); //if it passes the while loop, you can assign it to that time
                    let index = matches.indexOf(match);
                    matches.splice(index, 1); //remove the match
                    slotTeams.push(match.teamA, match.teamB); //put the teams into the list of teams scheduled for that time
                
                matches = skippedMatches.concat(matches); //once you get past the timeslot, add the skipped matches back
                table.push(slotMatches); //put the slot matches into the overall table
        
        this.setState(table);
    

我相信当列表中没有更多匹配项时会发生错误,并且控制台会给出此错误: 类型错误:匹配未定义。 我一直在想办法在所有比赛都排序后结束,但我现在迷路了。

编辑: 更改为以下答案中的代码后,它会运行,但某些匹配项会多次填充到表中:

【问题讨论】:

【参考方案1】:

解决问题的最简单方法是在运行“while”循环之前检查是否存在匹配项。看起来像这样:

assignTime()
  let matches=this.state.matches;
  console.log(matches);

  let table = [];
  let timeslots = this.state.timeLabels;
  console.log(timeslots);
  let pitches = this.state.pitches;
  console.log(pitches);

  for (let slot =0; slot<timeslots.length; slot++) //this code block runs through every time slot
      let slotMatches=[];
      let slotTeams= [];
      let skippedMatches =[];
      for (let pitchNo = 0; pitchNo<pitches.length; pitchNo++)  //running through the available pitches
          let match = matches[0];//getting first match from the list

          if (match) 
                while (slotTeams.includes(match.teamA) || slotTeams.includes(match.teamB))  //if one of the teams is already scheduled for that slot
                    skippedMatches.push(match); //skip this match
                    let index = matches.indexOf(match);
                    matches.splice(index, 1); //remove that match temporarily from the list
                    match = matches[0]; //get the next match up
                
                slotMatches.push(match); //if it passes the while loop, you can assign it to that time
                let index = matches.indexOf(match);
                matches.splice(index, 1); //remove the match
                slotTeams.push(match.teamA, match.teamB); //put the teams into the list of teams scheduled for that time
            
            matches = skippedMatches.concat(matches); //once you get past the timeslot, add the skipped matches back
            table.push(slotMatches); //put the slot matches into the overall table
        
  
  this.setState(table);

但是,一般而言,您的代码可以大大简化,这表明您可能需要复习一些 javascript 基础知识。编码愉快!

【讨论】:

谢谢,我对 JavaScript 比较陌生,所以我想先用我能理解的方式来做。一旦我确信它有效,我将尝试简化它。除了将 for 循环转换为 for each 循环之外,您有什么可以简化的建议吗? 它现在可以工作了,但是如果我运行它两次就会崩溃,我相信它很容易修复。谢谢!

以上是关于格式化 javascript 函数以将匹配映射到时间组的主要内容,如果未能解决你的问题,请参考以下文章

如何定义 swig 类型映射以将 unsigned char* 返回到 java

在 XSLT 中调用 str_contains 或任何 PHP 函数以将字符串与模式匹配

将 PHP 日期格式映射到 Javascript [重复]

如何从 SQL where 子句创建一个 JavaScript 函数,以将其作为谓词传递给 JavaScript 数组的过滤函数?

如何将 JavaScript 日期转换为 MySQL 日期时间格式? [复制]

函数总结