You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React+JavaScript彩票系统:统计最常见投注组合的实现难题

彩票系统投注组合统计问题

我用JavaScript和React开发练手用的彩票系统,现在卡在函数编写上:需要检查betObject数组,找出最常见的投注组合,并列出所有该组合的投注记录(后续用于计算总投注金额)。

投注规则

  • 投注分为两类:
    • pair:投注组合为2个数字
    • shuffle:投注组合为3-6个数字
  • 开奖结果是2个数字的组合

当前困境

尝试过用filter、map方法实现,也考虑过for循环但不知如何落地,还试过转换bet.combination的类型(数字/字符串)但未成功。

投注数组结构

每个投注对象包含:

  • combination:数组类型,存储投注数字
  • betTotal:投注金额数值
  • type:投注类型(pair/shuffle)

现有代码

const betsWatchList = () => {
    // Create watchlist array to store common bets array
    let watchlist = [];
    // loop through watchlist 
    for(let j = 0; j < watchlist.length; j++){
        //map through betList
        betList.map((bet, i) => {
            // if betlist combination matches watchlist
            if (watchlist[j].combination == bet.combination){
                // create new watchobject based on original
                let watchObject = {
                    combination : bet.combination,
                    total : watchlist[j].combination + bet.betTotal,
                    count : watchlist[j].count++
                }
                //replace original by updated
                watchlist.splice(j, watchObject);
            }else{
                //create new watchobject
                let watchObject = {
                    combination : bet.combination,
                    total : bet.betTotal,
                    count: 1
                }
                //push new watchobject
                watchlist.push(watchObject)
            }
        })
    } 
};

问题分析与解决方案

现有代码的核心问题

  1. 初始watchlist为空数组,外层for循环根本不会执行
  2. 直接用==比较数组永远不相等(数组是引用类型)
  3. 统计总金额时错误地将数组与数值相加,逻辑完全错误

正确实现思路与代码

const analyzeBets = (betList) => {
    // 1. 统计每个投注组合的出现次数、总金额及对应投注记录
    const combinationStats = {};

    betList.forEach(bet => {
        // 将数组转为唯一字符串:排序后用逗号连接,避免pair型投注因数字顺序被误判为不同组合
        // 若业务中数字顺序有意义,可去掉sort()
        const comboKey = bet.combination.sort((a, b) => a - b).join(',');
        
        if (combinationStats[comboKey]) {
            // 已有该组合,更新统计数据
            combinationStats[comboKey].count += 1;
            combinationStats[comboKey].total += bet.betTotal;
            combinationStats[comboKey].bets.push(bet);
        } else {
            // 首次出现该组合,初始化统计项
            combinationStats[comboKey] = {
                originalCombination: bet.combination,
                count: 1,
                total: bet.betTotal,
                bets: [bet]
            };
        }
    });

    // 2. 找出出现次数最多的组合
    let maxCount = 0;
    let mostCommonCombo = null;

    Object.values(combinationStats).forEach(stat => {
        if (stat.count > maxCount) {
            maxCount = stat.count;
            mostCommonCombo = stat;
        }
    });

    return {
        mostCommon: mostCommonCombo,
        allStats: combinationStats
    };
};

// 使用示例
const analysisResult = analyzeBets(betList);
console.log('最常见投注组合:', analysisResult.mostCommon.originalCombination);
console.log('出现次数:', analysisResult.mostCommon.count);
console.log('总投注金额:', analysisResult.mostCommon.total);
console.log('所有该组合的投注记录:', analysisResult.mostCommon.bets);

思路调整建议

  1. 用对象/Map替代数组存统计结果:对象键可直接作为组合的唯一标识,比遍历数组匹配高效得多
  2. 解决数组比较问题:将数组转为排序后的字符串,确保相同数字组合(无论顺序)被视为同一统计项
  3. 单次遍历完成统计:避免嵌套循环,一次forEach即可完成所有统计,性能更优
  4. 分离统计与查找逻辑:先完成全量统计,再从中筛选最常见组合,逻辑清晰且便于扩展(如查找前N个高频组合)

内容的提问来源于stack exchange,提问作者Danil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 22:57:57