2

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;
};
1
  • map() always returns a value for each element of the input array. Use a for loop or reduce(), then you can decide whether to add an element to the result array. Commented May 4, 2022 at 0:14

3 Answers 3

2

You can use Array.reduce instead of Array.map to only add valid games. I am assuming games with error go in default switch case, so there I set tmp to false. If tmp has data (is not false) then add the game to results array.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Update : changed the code from using Array.filter to Array.reduce as reduce is more suitable in this case and the result of Array.filter was not as expected.
1

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.

Comments

0

You can just filter the result to only truthy array items by doing .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;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.