JavaScript-通过数组映射,但仅在没有错误的情况下返回值
基本上,我有一系列我正在绘制的游戏。有时游戏缺少数据,这会导致我的代码中的错误。我需要忽略这些游戏,而不要将它们返回到映射的阵列中。我尝试使用以前看到的尝试捕获块 - 确实可以防止代码错误,但它将“未定义”返回到映射的数组中。
我该如何达到排除这些游戏的渴望?
// function to test record bet and result for each game
const checkBets = (gamelogs, bet) => {
let checked = gamelogs.map(game =>{
let tmp = {};
try{
switch(bet){
// handle money line bets
case('home.ml'):
tmp.bet = game.home.team+" ML";
tmp.odds = game.home.ml.open;
if(game.home.score > game.away.score){
tmp.result = "W";
}else if(game.home.score < game.away.score){
tmp.result = "L";
}else tmp.result = "P";
break;
case('away.ml'):
tmp.bet = game.away.team+" ML";
tmp.odds = game.away.ml.open;
if(game.away.score > game.home.score){
tmp.result = "W";
}else if(game.away.score < game.home.score){
tmp.result = "L";
}else tmp.result = "P";
break;
// handle runline bets
case('home.runline'):
tmp.bet = game.home.team + " " + game.home.runline.runs;
tmp.odds = game.home.runline.odds;
if(game.home.score + game.home.runline.runs > game.away.score){
tmp.result = "W";
}else if(game.home.score + game.home.runline.runs < game.away.score){
tmp.result = "L";
}else tmp.result ="P";
break;
case('away.runline'):
tmp.bet = game.away.team + " " + game.away.runline.runs;
tmp.odds = game.away.runline.odds;
if(game.away.score + game.away.runline.runs > game.home.score){
tmp.result = "W";
}else if(game.away.score + game.away.runline.runs < game.home.score){
tmp.result = "L";
}else tmp.result ="P";
break;
// handle total bets
case('over'):
tmp.bet = "O " + game.totals.open.runs;
tmp.odds = game.totals.open.odds;
if(game.home.score + game.away.score > game.totals.open.runs){
tmp.result = "W";
}else if(game.home.score + game.away.score < game.totals.open.runs){
tmp.result = "L";
}else tmp.result ="P";
break;
case('under'):
tmp.bet = "U " + game.totals.open.runs;
tmp.odds = game.totals.open.odds;
if(game.home.score + game.away.score < game.totals.open.runs){
tmp.result = "W";
}else if(game.home.score + game.away.score > game.totals.open.runs){
tmp.result = "L";
}else tmp.result ="P";
break;
default:
break;
};
// return game
return {...game, bet:tmp};
}catch(err){
console.log(err);
return;
};
});
return checked;
};
Basically I have an array of games that I am mapping through. Sometimes the games are missing data, which causes errors in my code. I need to ignore these games and not return them to the mapped array. I tried using a try catch block as seen before - which does prevent the code from erroring out, but it returns 'undefined' to the mapped array.
How can I achieve my desire of excluding these games?
// function to test record bet and result for each game
const checkBets = (gamelogs, bet) => {
let checked = gamelogs.map(game =>{
let tmp = {};
try{
switch(bet){
// handle money line bets
case('home.ml'):
tmp.bet = game.home.team+" ML";
tmp.odds = game.home.ml.open;
if(game.home.score > game.away.score){
tmp.result = "W";
}else if(game.home.score < game.away.score){
tmp.result = "L";
}else tmp.result = "P";
break;
case('away.ml'):
tmp.bet = game.away.team+" ML";
tmp.odds = game.away.ml.open;
if(game.away.score > game.home.score){
tmp.result = "W";
}else if(game.away.score < game.home.score){
tmp.result = "L";
}else tmp.result = "P";
break;
// handle runline bets
case('home.runline'):
tmp.bet = game.home.team + " " + game.home.runline.runs;
tmp.odds = game.home.runline.odds;
if(game.home.score + game.home.runline.runs > game.away.score){
tmp.result = "W";
}else if(game.home.score + game.home.runline.runs < game.away.score){
tmp.result = "L";
}else tmp.result ="P";
break;
case('away.runline'):
tmp.bet = game.away.team + " " + game.away.runline.runs;
tmp.odds = game.away.runline.odds;
if(game.away.score + game.away.runline.runs > game.home.score){
tmp.result = "W";
}else if(game.away.score + game.away.runline.runs < game.home.score){
tmp.result = "L";
}else tmp.result ="P";
break;
// handle total bets
case('over'):
tmp.bet = "O " + game.totals.open.runs;
tmp.odds = game.totals.open.odds;
if(game.home.score + game.away.score > game.totals.open.runs){
tmp.result = "W";
}else if(game.home.score + game.away.score < game.totals.open.runs){
tmp.result = "L";
}else tmp.result ="P";
break;
case('under'):
tmp.bet = "U " + game.totals.open.runs;
tmp.odds = game.totals.open.odds;
if(game.home.score + game.away.score < game.totals.open.runs){
tmp.result = "W";
}else if(game.home.score + game.away.score > game.totals.open.runs){
tmp.result = "L";
}else tmp.result ="P";
break;
default:
break;
};
// return game
return {...game, bet:tmp};
}catch(err){
console.log(err);
return;
};
});
return checked;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
array.reduce
而不是array.map
仅添加有效的游戏。我假设带有错误的游戏属于默认
开关案例,因此我将tmp
设置为false。如果TMP
具有数据(不是错误),则将游戏添加到结果数组中。You can use
Array.reduce
instead ofArray.map
to only add valid games. I am assuming games with error go indefault
switch case, so there I settmp
to false. Iftmp
has data (is not false) then add the game to results array.返回checked.filter(boolean);
这将确保您的数组中没有
不确定的
。没有gamelogs
的内容,这就是我现在可以弄清楚的。return checked.filter(Boolean);
This will make sure that there is no
undefined
in your array. Without the contents of thegamelogs
, this is all I could figure out right now.您可以通过执行
.filter(boolean)
仅将结果过滤到真实的数组项目中:You can just filter the result to only truthy array items by doing
.filter(Boolean)
: