Home > Net >  Create new arrays from values of an existing array
Create new arrays from values of an existing array

Time:07-13

I have an array with names

const players = ['name1','name2','name3','name4','name5','name6','name7','name8'];

const teams = players.length / 2; // -> 4 teams

I want to make teams of 2 people (randomly chosen) and make new arrays from the results -> team

function createTeams() {
  for (let i = 0; i < teams; i  ) {
    for (let x = 0; x < 2; x  ) {
      // get a random player
      selectedPlayer = players[Math.floor(Math.random() * players.length)];
      console.log('Selected Player will be added to a team: ', selectedPlayer);

      // delete this player from the array
      while (players.indexOf(selectedPlayer) !== -1) {
        players.splice(players.indexOf(selectedPlayer), 1);
      }

      // add this player to a new array
      //?????
    }
  }
}

Anyone knows how to do this?

CodePudding user response:

You can define a new Array which will contains the teams where you push the two players.

Note that it's better to pass the players array as a parameters of the function and make a swallow copy of it so it won't modify the player array.

const players = ['name1','name2','name3','name4','name5','name6','name7','name8'];

function createTeams(players) {
  const playersLeft = [...players]
  const newTeams = []  
  const nbTeams = players.length / 2
  for (let i=0; i<nbTeams; i  ){
    const player1 = playersLeft.splice(Math.floor(Math.random()*playersLeft.length),1)[0]
    const player2 = playersLeft.splice(Math.floor(Math.random()*playersLeft.length),1)[0]
    
    newTeams.push([player1, player2])
  }
  
  return newTeams
}

console.log(createTeams(players))

Edit : Improve version using a nbPlayerPerTeam parameter

const players = ['name1', 'name2', 'name3', 'name4', 'name5', 'name6', 'name7', 'name8'];

function createTeams(players, nbPlayerPerTeam) {
  const playersLeft = [...players]
  const newTeams = []
  const nbTeams = players.length / nbPlayerPerTeam
  for (let i = 0; i < nbTeams; i  ) {
    const players = []
    for (let j = 0; j < nbPlayerPerTeam; j  ) {
      const player = playersLeft.splice(Math.floor(Math.random() * playersLeft.length), 1)[0]

      players.push(player)

    }


    newTeams.push(players)
  }

  return newTeams
}

console.log(createTeams(players, 3))

CodePudding user response:

function pickTeams(array, teamSize) {

  // Shuffle array, make a copy to not alter original our original array
  const shuffled = [...array].sort( () => 0.5 - Math.random());

  const teams = [];
  // Create teams according to a team size
  for (let i = 0; i < shuffled.length; i  = teamSize) {
    const chunk = shuffled.slice(i, i   teamSize);
    teams.push(chunk);
  }
  
  return teams;
  
}

const players = ['name1','name2','name3','name4','name5','name6','name7','name8'];

const teams = pickTeams(players, 2);

CodePudding user response:

const players = ['name1', 'name2', 'name3', 'name4', 'name5', 'name6', 'name7', 'name8'];

// Divide data by length
const divide = (arr, n) => arr.reduce((r, e, i) =>
  (i % n ? r[r.length - 1].push(e) : r.push([e])) && r, []);

const shuffle = (arr) => {
  // Deep copy an array using structuredClone
  const array = structuredClone(arr);
  let currentIndex = array.length, randomIndex;
  // While there remain elements to shuffle
  while (currentIndex != 0) {
    // Pick a remaining element
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex--;
    // And swap it with the current element
    [array[currentIndex], array[randomIndex]] = [
      array[randomIndex], array[currentIndex]
    ];
  }
  return array;
}

// Shuffle players
const shuffled = shuffle(players);

// Set a number of people in a team
const groups = divide(shuffled, players.length / 2);

groups.forEach((team, i) => console.log(`team ${i 1}`, JSON.stringify(team)));

CodePudding user response:

Check this updated code, I hope it will work for you. Just create two dimensional array and push players.

    const players = ['name1','name2','name3','name4','name5','name6','name7','name8'];
    let multi_team=new Array;     // new array for team of random 2 players
    const teams = players.length / 2; // -> 4 teams
    console.log(teams);
    createTeams();
    function createTeams() {
        for (let i = 0; i < teams; i  ) {
            multi_team[i]= new Array;
            for (let x = 0; x < 2; x  ) {
                // get a random player
                selectedPlayer = players[Math.floor(Math.random() * players.length)];
                multi_team[i][x]=selectedPlayer;
                 // delete this player from the array
                while (players.indexOf(selectedPlayer) !== -1) {
                    players.splice(players.indexOf(selectedPlayer), 1);
                }

            }
        }
        console.log(multi_team);
    }

CodePudding user response:

To differ from previous answers, this is a dynamic way to do it, so you don't care if there are 5 teams or 2, 10 players or 500.

const players = ['j1', 'j2', 'j3', 'j4', 'j5', 'j6', 'j7', 'j8'];

const organizeTeams = (numberOfTeams, players) => {
  var teams = [];
  // Clone the array to avoid mutations
  const freePlayers = [...players];
  // How many players will play per team
  const playersPerTeam = Math.floor(players.length / numberOfTeams);
  // How many player won't have team
  const unorganizablePlayers = freePlayers.length % numberOfTeams;

  for (let t = 1; t <= numberOfTeams; t  ) {
    teams = [...teams, pickRandomPlayers(freePlayers, playersPerTeam)];
  }

  return { teams, playersPerTeam, unorganizablePlayers };
};

const pickRandomPlayers = (players, playersPerTeam) => {
  let pickedPlayers = [];

  for (let c = 1; c <= playersPerTeam; c  ) {
    const index = Math.floor(Math.random() * (players.length - 1));
    const player = players[index];

    pickedPlayers = [...pickedPlayers, player];

    // When we pick the player we remove it from the array to avoid duplicates.
    players.splice(index, 1);
  }

  return pickedPlayers;
};

const championship = organizeTeams(3, players);

console.log(`We have ${championship.teams.length} teams.`);
championship.teams.forEach((team, index) => {
  console.log(`Team ${index   1} players: ${team.join(',')}`);
});
console.log(
  `There was no place to assign ${championship.unorganizablePlayers} players`
);

CodePudding user response:

const players = [ 'name1', 'name2', 'name3', 'name4', 'name5', 'name6', 'name7', 'name8' ]

const shuffled = players.sort((a, b) => 0.5 - Math.random())

const result = Array.from({ length: shuffled.length / 2 }, (_, i) => shuffled.slice(i * 2, i * 2   2));

console.log(result)

  • Related