0

I have thought about this alot but i cant find a good solution..that is also fast in Javascript. I have an array of objects..the objects are game searches for a random player. The array may look like this:

const GameSearches[
{fromPlayerId:"378329",goalScore:20}
{fromPlayerId:"125342",goalScore:20}
{fromPlayerId:"378329",goalScore:20}
{fromPlayerId:"918273",goalScore:20}
{fromPlayerId:"378329",goalScore:20}
]

In this array i need to rund a function called CreateNewGame(Player1,Player2). In this array i could create games with for example index 0 and 1. Index 2 and 3. Index 4 would be left in the array as there are no more players to match on.

Anyone got a good solution to this? It would really help me out.

I have tried different filter and map without finding a good solution. The output should call a function createnewgame

Example:

createNewGame(GameSearches[0].from,GameSearches[1].from)

this function will be called as there are two players looking for a game. they do not have they same fromPlayerId so they should match.

I see some comments on that StackOverflow is not a free codingservice..the app has thousands of lines..this is only a small part. Im asking becouse i cant figure out the logic on how to to this. I do not need a full working example.

ThorEpNorway
  • 95
  • 10
  • I believe https://stackoverflow.com/questions/8495687/split-array-into-chunks is what you're looking for. You want to chunk an array. Unfortunately it is not something that javascript has out of the box, but you can create it yourself, with the example I linked. – Douwe de Haan May 31 '21 at 09:34
  • Can you add a code sample to how the output should look? – Dor Ben Itzhak May 31 '21 at 09:35
  • I would use a simple `for` loop where you increment the index by two `i+=2` – anotherOne May 31 '21 at 09:35
  • 1
    Sheik Yerbouti The for loop would work..but what if index 0 and 5 need to match.. I will give it a go – ThorEpNorway May 31 '21 at 09:42

2 Answers2

0

I think maybe i can use the suggestion of a for loop..and it will work fine.

for (let i = 0; i < games20GoalScore.length; i = i + 2) {
    if (games20GoalScore[i + 1] !== undefined) {
      console.log(games20GoalScore[i] + " : " + games20GoalScore[i + 1]);
      if (games20GoalScore[i].from !== games20GoalScore[i + 1].from) {
        console.log("Match");
      }
    }
  }

This code is run each time the array get a new item.

ThorEpNorway
  • 95
  • 10
0

You can try something like this

const GameSearches = [
    {fromPlayerId:"378329",goalScore:20},
    {fromPlayerId:"125342",goalScore:20},
    {fromPlayerId:"378329",goalScore:20},
    {fromPlayerId:"918273",goalScore:20},
    {fromPlayerId:"378329",goalScore:20}
    ];

    const createNewGames = (player1, player2) =>           console.log(player1.fromPlayerId, player2.fromPlayerId)

    const getMatch = (GameSearches) => {
     while([...new Set(GameSearches)].length > 1){
       const player1 = GameSearches[0];
       GameSearches.shift();
       const player2Index = GameSearches.findIndex(el => el.fromPlayerId !== player1.fromPlayerId)
       const player2 = GameSearches[player2Index];
       GameSearches.splice(player2Index,1)
       createNewGames(player1, player2);
     }
    }

getMatch(GameSearches);
Raycas
  • 151
  • 1
  • 8
  • This is giving some strange results...it works..but i have to put two new game request from same player before it matches it with another player – ThorEpNorway Jun 02 '21 at 06:26