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;
};
return checked.filter(Boolean);
This will make sure that there is no undefined
in your array. Without the contents of the gamelogs
, this is all I could figure out right now.