Javascript -映射数组,但只有在没有错误时才返回值



基本上,我有一组我要映射的游戏。有时游戏缺少数据,这会导致我的代码出错。我需要忽略这些游戏,不将它们返回到映射数组。我尝试使用前面看到的try catch块-这确实可以防止代码出错,但它返回'undefined'到映射的数组。

我怎样才能实现排除这些游戏的愿望?

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

您可以使用Array.reduce而不是Array.map来只添加有效的游戏。我假设在default开关情况下有错误的游戏,所以我将tmp设置为false。如果tmp有数据(不是假的),那么将游戏添加到结果数组。

// function to test record bet and result for each game
const checkBets = (gamelogs, bet) => {
let checked = gamelogs.reduce((result, 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:
// Set tmp to false in case of error
tmp = false;
break;
};

// If tmp has data (valid game), add this game to result array
if (tmp) result.push(tmp);

return result;
}catch(err){
console.log(err);
return;
};
}, []);
return checked;
};

return checked.filter(Boolean);

这将确保数组中没有undefined。如果没有gamelogs的内容,这就是我现在所能想到的。

您可以通过执行.filter(Boolean):

将结果筛选为只有真实的数组项
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;
// ...rests of cases...
default:
break;
}
// return game
return { ...game, bet: tmp };
} catch (err) {
console.log(err);
return;
}
});
// Filter to only truthy results;
checked = checked.filter(Boolean);
return checked;
};

最新更新