gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:15:38 25 4
gpt4 key购买 nike

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

 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});
}

我相信当列表中没有更多匹配项时会发生错误,并且控制台会给出以下错误:类型错误:匹配未定义。我一直在尝试找出一种方法来结束所有匹配项的排序,但我现在迷失了方向。

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

最佳答案

解决问题的最简单方法是在运行“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 - 格式化 JavaScript 函数以将匹配项映射到时间组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61146873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com