Javascript - 通过数组映射,但如果没有错误则只有 return 值

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

基本上我有一系列的游戏,我正在映射。有时游戏会丢失数据,这会导致我的代码出错。我需要忽略这些游戏,而不是 return 它们到映射数组。我尝试使用之前看到的 try catch 块 - 这确实可以防止代码出错,但它 returns '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;
};

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

您可以使用 Array.reduce 而不是 Array.map 来只添加有效的游戏。我假设有错误的游戏进入 default switch case,所以我将 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;
};