Home > Enterprise >  Javascript - map through array but only return value if there is no error
Javascript - map through array but only return value if there is no error

Time:05-04

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

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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;
};
  • Related