javascript 查找对象数组中比较键值的所有可能性

huwehgph  于 2022-12-25  发布在  Java
关注(0)|答案(1)|浏览(122)

我有一个特定的数学公式,它有三个参数,我试图从一个对象数组中得到,运行这个数学公式的所有可能性。
这是关于体育的。想象一场比赛有3种可能性:A队(赢)-B队(赢)-平局。3个投注网站正在处理这个事件。但是他们中的3个对这场比赛有不同的赔率值。
我想运行这3个投注网站,以获得所有的可能性,我可以为这个事件。从来没有得到一个以上的奇数从同一个投注网站。
示例:

  • 网站A:A队(胜)
  • 网站B:B队(获胜)
  • 网站C:抽奖

我使用JavaScript来完成这个任务。提前感谢您的时间和支持。
真的很感激。
这里是一个数据的例子,我必须得到这些可能性。每个对象是一个网站,进入对象,赔率是对关键的"结果"。对象数组有3个对象,但它可以有更多

[
    {
        "key": "betmgm",
        "title": "BetMGM",
        "last_update": "2022-12-14T04:30:40Z",
        "markets": [
            {
                "key": "h2h",
                "outcomes": [
                    {
                        "name": "AC Milan",
                        "price": 138
                    },
                    {
                        "name": "Tottenham Hotspur",
                        "price": 200
                    },
                    {
                        "name": "Draw",
                        "price": 225
                    }
                ]
            }
        ]
    },
    {
        "key": "barstool",
        "title": "Barstool Sportsbook",
        "last_update": "2022-12-14T04:30:22Z",
        "markets": [
            {
                "key": "h2h",
                "outcomes": [
                    {
                        "name": "AC Milan",
                        "price": 130
                    },
                    {
                        "name": "Tottenham Hotspur",
                        "price": 220
                    },
                    {
                        "name": "Draw",
                        "price": 230
                    }
                ]
            }
        ]
    },
    {
        "key": "twinspires",
        "title": "TwinSpires",
        "last_update": "2022-12-14T04:17:45Z",
        "markets": [
            {
                "key": "h2h",
                "outcomes": [
                    {
                        "name": "AC Milan",
                        "price": 130
                    },
                    {
                        "name": "Tottenham Hotspur",
                        "price": 220
                    },
                    {
                        "name": "Draw",
                        "price": 230
                    }
                ]
            }
        ]
    }
]

我希望接收一个具有如下可能性的数组:

[
{
  "bookMaker": "TwinSpires",
  "name": "Tottenham Hotspur",
  "price": 230,
},
{
  "bookMaker": "Barstool Sportsbook",
  "name": "AC Milan",
  "price": 130,
},
{
  "bookMaker": "BetMGM",
  "name": "Draw",
  "price": 225,
}
]
wtzytmuj

wtzytmuj1#

算法很简单,首先,我们取第一个投注网站的所有结果,随机洗牌。
然后,我们简单地循环JSON对象,它是一个下注网站列表,更不用说结果的数量必须与下注网站的数量相同,我们为每个下注网站分配下一个洗牌结果,并将其记录到控制台。
算法如下:

var data = [
    {
        "key": "betmgm",
        "title": "BetMGM",
        "last_update": "2022-12-14T04:30:40Z",
        "markets": [
            {
                "key": "h2h",
                "outcomes": [
                    {
                        "name": "AC Milan",
                        "price": 138
                    },
                    {
                        "name": "Tottenham Hotspur",
                        "price": 200
                    },
                    {
                        "name": "Draw",
                        "price": 225
                    }
                ]
            }
        ]
    },
    {
        "key": "barstool",
        "title": "Barstool Sportsbook",
        "last_update": "2022-12-14T04:30:22Z",
        "markets": [
            {
                "key": "h2h",
                "outcomes": [
                    {
                        "name": "AC Milan",
                        "price": 130
                    },
                    {
                        "name": "Tottenham Hotspur",
                        "price": 220
                    },
                    {
                        "name": "Draw",
                        "price": 230
                    }
                ]
            }
        ]
    },
    {
        "key": "twinspires",
        "title": "TwinSpires",
        "last_update": "2022-12-14T04:17:45Z",
        "markets": [
            {
                "key": "h2h",
                "outcomes": [
                    {
                        "name": "AC Milan",
                        "price": 130
                    },
                    {
                        "name": "Tottenham Hotspur",
                        "price": 220
                    },
                    {
                        "name": "Draw",
                        "price": 230
                    }
                ]
            }
        ]
    }
];

start(data);

function start(data) {
    var outcomes = data[0].markets[0].outcomes;

    if (data.length != outcomes.length) {
        alert("Invalid data!");

        return;
    }

    var results = createResults(data);

    printResults(results);
}

function createResults(data) {
    var outcomes = data[0].markets[0].outcomes;
    var newOutcomesNames = shuffleOutcomes(outcomes);
    
    var results = [];

    for (var i = 0; i < data.length; i++) {
        var currentBetWebsite = data[i];
        var currentResult = {};
        var currentOutcomes = currentBetWebsite.markets[0].outcomes;

        currentResult.bookMaker = currentBetWebsite.title;
        currentResult.name = newOutcomesNames[i];

        for (var j = 0; j < newOutcomesNames.length; j++) {
            if (newOutcomesNames[i] == currentOutcomes[j].name) {
                currentResult.price = currentOutcomes[j].price;
            }
        }

        results.push(currentResult);
    }

    return results;
}

function printResults(results) {
    console.log(results);
}

function shuffleOutcomes(outcomes) {
    var parsedOutcomes = parseOutcomesNames(outcomes);
    var outcomesLength = parsedOutcomes.length;
    var newOutcomes = [];
    
    for (var i = 0; i < outcomesLength; i++) {
        var random = Math.floor(Math.random() * (outcomesLength - i));

        newOutcomes.push(parsedOutcomes[random]);
        parsedOutcomes.splice(random, 1);
    }

    return newOutcomes;
}

function parseOutcomesNames(outcomes) {
    var outcomesNames = outcomes.map(o => o.name);

    return outcomesNames;
}

如果你需要任何帮助,请告诉我。

相关问题