0

I've implemented during my JS training a suggestion on how to get an array based on some json data and criteria. The code works fine, but the generated arrays are always the same. I want to generate random arrays using all the element of the original array, I have this data structure:

//THIS IS THE ARRAY THAT IS GENERATED FROM THE ORIGINAL DATASET DIVIDED BY TEAM. EVERY TEAM HAS 24 / 33 ELEMENTS MAX

Array(20)
0: Array(26)
0: {player_id: 1, player_surname: "Ronaldo", player_name: "Cristiano", player_number: 7, role_id: 8, …}
1: {player_id: 7, player_surname: "Higuain", player_name: "Gonzalo", player_number: 9, role_id: 8, …}
2: {player_id: 5, player_surname: "Dybala", player_name: "Paulo", player_number: 10, role_id: 8, …}
3: {player_id: 10, player_surname: "Mandzukic", player_name: "Mario", player_number: 17, role_id: 8, …}
4: {player_id: 58, player_surname: "Costa", player_name: "Douglas", player_number: 11, role_id: 7, …}
5: {player_id: 66, player_surname: "Pjanic", player_name: "Miralem", player_number: 5, role_id: 7, …}
6: {player_id: 14272, player_surname: "Ramsey", player_name: "Aaron", player_number: 8, role_id: 7, …}
7: {player_id: 14367, player_surname: "De Ligt", player_name: "Matthijs", player_number: 4, role_id: 6, …}
8: {player_id: 85, player_surname: "Bernardeschi", player_name: "Federico", player_number: 33, role_id: 7, …}
9: {player_id: 14273, player_surname: "Rabiot", player_name: "Adrien", player_number: 25, role_id: 7, …}
10: {player_id: 150, player_surname: "Chiellini", player_name: "Giorgio", player_number: 3, role_id: 6, …}
11: {player_id: 91, player_surname: "Szczesny", player_name: "Wojciech", player_number: 1, role_id: 5, …}
12: {player_id: 135, player_surname: "Bonucci", player_name: "Leonardo", player_number: 19, role_id: 6, …}
13: {player_id: 14449, player_surname: "Danilo", player_name: ".", player_number: 0, role_id: 6, …}
14: {player_id: 109, player_surname: "Can", player_name: "Emre", player_number: 23, role_id: 7, …}
15: {player_id: 106, player_surname: "Sandro", player_name: "Alex", player_number: 12, role_id: 6, …}
16: {player_id: 211, player_surname: "Bentancur", player_name: "Rodrigo", player_number: 30, role_id: 7, …}
17: {player_id: 89, player_surname: "Khedira", player_name: "Sami", player_number: 6, role_id: 7, …}
18: {player_id: 97, player_surname: "Matuidi", player_name: "Blaise", player_number: 14, role_id: 7, …}
19: {player_id: 90, player_surname: "Cuadrado", player_name: "Juan", player_number: 16, role_id: 7, …}
20: {player_id: 14227, player_surname: "Demiral", player_name: "Merih", player_number: 3, role_id: 6, …}
21: {player_id: 14274, player_surname: "Buffon", player_name: "Gianluigi", player_number: 77, role_id: 5, …}
22: {player_id: 219, player_surname: "Rugani", player_name: "Daniele", player_number: 24, role_id: 6, …}
23: {player_id: 266, player_surname: "De Sciglio", player_name: "Mattia", player_number: 2, role_id: 6, …}
24: {player_id: 52, player_surname: "Pjaca", player_name: "Marko", player_number: 0, role_id: 8, …}
25: {player_id: 537, player_surname: "Pinsoglio", player_name: "Carlo", player_number: 21, role_id: 5, …}
length: 26
__proto__: Array(0)

// there are other 19 elements for a total of 20 full teams of 24/33 players
//THIS IS THE ARRAY THAT IS CREATED FOR EVERY TEAM AFTER THAT THE DATASET IS PROCESSED. IT WILL ALWAYS CREATE THE SAME TEAMS, PLAYERS WILL NOT ROTATE. I NEED TO RANDOMIZE THIS
Array(20)
0: (11) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
1: Array(11)
0: {player_id: 3, player_surname: "Immobile", player_name: "Ciro", player_number: 17, role_id: 8, …}
1: {player_id: 14145, player_surname: "Correa", player_name: "Joaquin", player_number: 11, role_id: 8, …}
2: {player_id: 51, player_surname: "Milinkovic-Savic", player_name: "Sergej", player_number: 21, role_id: 7, …}
3: {player_id: 60, player_surname: "Luis Alberto", player_name: "", player_number: 10, role_id: 7, …}
4: {player_id: 172, player_surname: "Lazzari", player_name: "Manuel", player_number: 29, role_id: 7, …}
5: {player_id: 160, player_surname: "Acerbi", player_name: "Francesco", player_number: 33, role_id: 6, …}
6: {player_id: 151, player_surname: "Strakosha", player_name: "Thomas", player_number: 1, role_id: 5, …}
7: {player_id: 125, player_surname: "Parolo", player_name: "Marco", player_number: 16, role_id: 7, …}
8: {player_id: 14376, player_surname: "Vavro", player_name: "Denis", player_number: 0, role_id: 6, …}
9: {player_id: 224, player_surname: "Felipe", player_name: "Luiz", player_number: 3, role_id: 6, …}
10: {player_id: 246, player_surname: "Radu", player_name: "Stefan", player_number: 26, role_id: 6, …}
length: 11
__proto__: Array(0)
2: Array(11)
0: {player_id: 408, player_surname: "Piatek", player_name: "Krzysztof", player_number: 9, role_id: 8, …}
1: {player_id: 14439, player_surname: "Leao", player_name: "Rafael", player_number: 17, role_id: 8, …}
2: {player_id: 19, player_surname: "Suso", player_name: "", player_number: 8, role_id: 7, …}
3: {player_id: 92, player_surname: "Kessié", player_name: "Franck", player_number: 79, role_id: 7, …}
4: {player_id: 14212, player_surname: "Paquetà", player_name: "Lucas", player_number: 39, role_id: 7, …}
5: {player_id: 339, player_surname: "Bennacer", player_name: "Ismael", player_number: 0, role_id: 7, …}
6: {player_id: 138, player_surname: "Donnarumma", player_name: "Gianluigi", player_number: 99, role_id: 5, …}
7: {player_id: 179, player_surname: "Romagnoli", player_name: "Alessio", player_number: 13, role_id: 6, …}
8: {player_id: 14448, player_surname: "Duarte", player_name: "Leo", player_number: 0, role_id: 6, …}
9: {player_id: 14370, player_surname: "Hernandez", player_name: "Theo", player_number: 19, role_id: 6, …}
10: {player_id: 293, player_surname: "Musacchio", player_name: "Mateo", player_number: 22, role_id: 6, …}
length: 11
__proto__: Array(0)

// The teams will be composed of the first elements found in a role and until it's not filled. This will cause that the result teams at every team execution will load always the same 11 team members.  

See this SO question to have more reference about the data processing code: The function will create some arrays according to the numbers and positions needed of the players present in each team.

Array.prototype.randomElement = function () {
    return this[Math.floor(Math.random() * this.length)]
}

$.getJSON( '../api/players_.json', function(players){

  const playersPerTeam = Object.values(players.reduce(function(acc, player){
       const teamKey = player.team_abbreviation;

       if(!acc.hasOwnProperty(teamKey)){
           acc[teamKey] = [];
       }

       acc[teamKey].push(player);
       return acc;
   }, {}));
   console.log(playersPerTeam);

  //var randomTeam = shuffle(playersPerTeam);


   const chosenSetupPerTeam = playersPerTeam.map(function(playersInTeam){


       const playersNeededPerRole = {
           "Portiere": 1, // Keeper
           "Difensore": 4, // Defender
           "Centrocampista": 4, // midfielder
           "Attaccante": 2, // Attacker
       };

       const playersPresentPerRole = {};

   //     // Get a team to fulfil the requirements stated in playersNeededPerRole
       return playersInTeam.filter(function(player, index, arr){
          //console.log(index);
           // Role does not exist, thus the player can't join the team
           if(!playersNeededPerRole.hasOwnProperty(player.role_name)){
               return false;
           }
           // Set the default of players present per role to 0
           if(!playersPresentPerRole.hasOwnProperty(player.role_name)){
               playersPresentPerRole[player.role_name] = 0;
           }
           // Stop if all positions have been filled as specified in playersNeededPerRole
           if(playersPresentPerRole[player.role_name] === playersNeededPerRole[player.role_name]){
               return false;
           }

           playersPresentPerRole[player.role_name]++;

           return true;
       });
   });

   console.log(chosenSetupPerTeam);

});

Any help to find a way to randomly extract the players that will compose the teams after they are grouped by position will be appreciated. I'm new to javascript so I'm not expert on all his functions. Thanks

jihuuNI
  • 551
  • 2
  • 5
  • 17
  • 1
    So pull out each set of players and shuffle it. https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array – epascarello Sep 03 '19 at 20:37
  • 2
    Possible duplicate of [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – Emiel Zuurbier Sep 03 '19 at 20:37
  • @EmielZuurbier I've readed that questions, but it's not fit my needings. I've thested the code solution provided but it will rotate only the teams order, not players! – jihuuNI Sep 03 '19 at 20:59
  • @epascarello Where is supposed that i can implement that solution in my code?before the array mapping or before the filter? – jihuuNI Sep 03 '19 at 21:00
  • 1
    @jihuuNI You need to randomize the players array first and then add them to the teams. – Emiel Zuurbier Sep 03 '19 at 21:18
  • @EmielZuurbier the player array is organized by player role. They are gropued by team and then assigned to the slot for every position in the function I have. You suggest me to rendomize the initial json player data array? – jihuuNI Sep 03 '19 at 21:42
  • 1
    Exactly. You have already implemented that the players get into the right team and that the roles get filled with players. So changing the order of JSON players data array *randomly* changes which player gets selected first, second, etc.. Random picking. – Emiel Zuurbier Sep 03 '19 at 22:01
  • So is the question: "How to build permutations from an array?"? – U. Windl Sep 03 '19 at 23:21

0 Answers0