JavaScript-通过数组映射,但仅在没有错误的情况下返回值

发布于 2025-01-25 22:19:01 字数 3535 浏览 2 评论 0原文

基本上,我有一系列我正在绘制的游戏。有时游戏缺少数据,这会导致我的代码中的错误。我需要忽略这些游戏,而不要将它们返回到映射的阵列中。我尝试使用以前看到的尝试捕获块 - 确实可以防止代码错误,但它将“未定义”返回到映射的数组中。

我该如何达到排除这些游戏的渴望?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

⒈起吃苦の倖褔 2025-02-01 22:19:01

您可以使用array.reduce而不是array.map仅添加有效的游戏。我假设带有错误的游戏属于默认开关案例,因此我将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;
};

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

默嘫て 2025-02-01 22:19:01

返回checked.filter(boolean);

这将确保您的数组中没有不确定的。没有gamelogs的内容,这就是我现在可以弄清楚的。

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.

王权女流氓 2025-02-01 22:19:01

您可以通过执行.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;
};

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;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文