计算对象数组中的对象属性在对象数组中的出现次数

huangapple go评论75阅读模式
英文:

Count iterations of an object array property in an array of objects

问题

我正在制作一个彩票系统,用来练习 JavaScript 和 React。

我在创建一个函数时遇到了困难,该函数将检查我的 betObject 数组中最常见的投注组合,并列出包含此组合的每个投注(以便稍后计算投注金额的总和)。

投注有两种类型:pair 和 shuffle。Pair 投注包括 2 个数字的组合,而 shuffle 投注包括 3 到 6 个数字的组合。开奖结果是一个 2 个数字的组合。

我尝试过使用 filter、map 和方程式来解决这个问题,我正在尝试使用 For 循环,但我不知道如何使用它来帮助解决我的问题。我还尝试过将投注组合传递给 Int 或 String,但没有成功。

betObject 数组的结构如下:

[![Bet Object Array log(int are purple, string are green)](https://i.stack.imgur.com/PMQ5J.png)](https://i.stack.imgur.com/PMQ5J.png)

以下是迄今为止的函数:

const betsWatchList = () => {
    // 创建一个观察列表数组以存储常见投注数组
    let watchlist = [];
    // 循环遍历观察列表
    for (let j = 0; j < watchlist.length; j++) {
        // 映射遍历 betList
        betList.map((bet, i) => {
            // 如果 betlist 的组合与 watchlist 匹配
            if (watchlist[j].combination == bet.combination) {
                // 基于原始创建新的 watchObject
                let watchObject = {
                    combination: bet.combination,
                    total: watchlist[j].combination + bet.betTotal,
                    count: watchlist[j].count++
                }
                // 用更新后的内容替换原始内容
                watchlist.splice(j, watchObject);
            } else {
                // 创建新的 watchObject
                let watchObject = {
                    combination: bet.combination,
                    total: bet.betTotal,
                    count: 1
                }
                // 推送新的 watchObject
                watchlist.push(watchObject)
            }
        })
    }
};

你应该如何处理这个问题?为了更快地找到解决方案,你应该对我的方法做出哪些改变?

谢谢!

英文:

I am making a lottery system to practice javascript and react.

I am having a hard time creating a function that will check my betObject array for the most common bet combination and list each bet featuring this combination(to make a sum of the bet amounts later).

Bets come in two types : pair and shuffle. Pair bets have a combination of 2 numbers while shuffle bets have a combination of 3 to 6 numbers. Result of the draw is a 2 number combination.

I have tried using filter, map and equations to make this work, I am trying to make use of a For loop but I can't see how to use it to help with my problem.I have also tried pasing the bet.combination to an Int or a String without success.

the betObject array is structured as follows :
计算对象数组中的对象属性在对象数组中的出现次数

Here is my function so far :

const betsWatchList = () =&gt; {
    // Create watchlist array to store common bets array
    let watchlist = [];
    // loop through watchlist 
    for(let j = 0; j &lt; watchlist.length; j++){
        //map through betList
        betList.map((bet, i) =&gt; {
            // 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)
            }
        })
    } 
};

How would you approach this problem ? What should I change about my approach to find the solution faster ?

Thank you !

答案1

得分: 0

以下是翻译好的部分:

不要使用 == 来测试两个数组是否包含相同的

尝试替换

if (watchlist[j].combination == bet.combination)

if (JSON.stringify(watchlist[j].combination) == JSON.stringify(bet.combination))

不要使用 .splice,为什么不直接写入数组元素?

代码中缺少 1,。应该改为 .splice(j, 1, watchObject)。然而,这都是不必要的复杂操作,实际上不需要使用 .splice,因为我们不会改变元素数量。

替换

watchlist.splice(j, watchObject);

watchlist[j] = watchObject;

没有理由使用 .map

.map 会让程序员期望循环代码返回结果,而我们可以使用 .forEach,这样可以明确表示不会返回结果。

循环顺序是相反的

外层循环应该遍历下注列表。对于每个下注,内层循环应该检查观察列表中的每个项目。

为了简化,我使用了 .forEach

这避免了创建和递增迭代器 i

function betsWatchList(betList) {
  const watchList = [];
  betList.forEach(bet => {
    let found = false;

    // 如果在 watchList 中已存在:更新
    watchList.forEach((watchItem, j) => {
      if (JSON.stringify(watchItem.combination) === JSON.stringify(bet.combination)) {
        watchItem.total += bet.betTotal;
        watchItem.count += 1;
        found = true;
      }
    });

    // 如果在 watchList 中不存在,追加新的 watchList 条目
    if (!found) {
      watchList.push({
        combination: bet.combination,
        total: bet.betTotal,
        count: 1
      });
    }
  })

  return watchList;
}

const betList = [{
  combination: [1, 2, 3],
  betTotal: 10
}, {
  combination: [1, 2, 3],
  betTotal: 20
}, {
  combination: [1, 2, 4],
  betTotal: 10
}]

console.log(betsWatchList(betList))
英文:

Don't use == to test whether two arrays contain identical values

Try replacing

 if (watchlist[j].combination == bet.combination)

with

  if (JSON.stringify(watchlist[j].combination) == JSON.stringify(bet.combination))

Instead of .splice, why not just write to the array element?

The code has a missing 1,. It should read .splice(j, 1, watchObject). However, it is all unnecessarily complicated, and doesn't actually need a .splice as we are not changing the number of elements.

Replace

watchlist.splice(j, watchObject); 

with

watchlist[j]= watchObject;

There was no reason to use .map

.map makes the programmer expect the code loop to return a result, when we could have used .forEach which makes it clear that it will not.

The loops are in reverse order

You need the outer loop to be over the bets. For each bet, you want the inner loop to examine each item in the watchList.

Just for simplicity, I have used .forEach

This avoids having to create and increment an iterator i.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function betsWatchList(betList) {
  const watchList = [];
  betList.forEach(bet =&gt; {
    let found = false;

    // If already exists in watchList: update
    watchList.forEach((watchItem, j) =&gt; {
      if (JSON.stringify(watchItem.combination) === JSON.stringify(bet.combination)) {
        watchItem.total += bet.betTotal
        watchItem.count += 1
        found = true
      }
    });


    // If doesn&#39;t exist in watchList, append new watchList entry
    if (!found) {
      watchList.push({
        combination: bet.combination,
        total: bet.betTotal,
        count: 1
      });
    }

  })

  return watchList
}


const betList = [{
  combination: [1, 2, 3],
  betTotal: 10
}, {
  combination: [1, 2, 3],
  betTotal: 20
}, {
  combination: [1, 2, 4],
  betTotal: 10
}]

console.log(betsWatchList(betList))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月12日 09:34:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定