从现有数组的值创建新数组



我有一个数组名称

const players = ['name1','name2','name3','name4','name5','name6','name7','name8'];
const teams = players.length / 2; // -> 4 teams

我想让2人的团队(随机选择),并从结果中制作新的数组->团队

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
//?????
}
}
}

有人知道怎么做吗?

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);

你可以定义一个新的数组,它将包含你推两个球员的球队。

请注意,最好将播放器数组作为函数的参数传递,并复制它,这样它就不会修改播放器数组。

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))

编辑:使用nbPlayerPerTeam参数改进版本

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))

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)));

检查这个更新的代码,我希望它将为您工作。只需创建二维数组并推送玩家。

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);
}

不同于以前的答案,这是一个动态的方式去做,所以你不在乎如果有5个团队或2,10名球员或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`
);

OP需要(实现)一个可靠的shuffle和同样一个数组的chunk功能。

只需shuffle提供的players数组(或后者的浅副本),并将随机排序的播放器项目数组划分为s,每个自定义提供的长度。

function shuffleArray(arr) {
let idx;
let count = arr.length;
while (--count) {
idx = Math.floor(Math.random() * (count + 1));
[arr[idx], arr[count]] = [arr[count], arr[idx]];
}
return arr;
}
function createListOfChunkLists(arr, chunkLength = 0) {
chunkLength = Math.max(0, chunkLength);
return (chunkLength === 0 && []) ||
arr.reduce((list, item, idx) => {
if (idx % chunkLength === 0) {
list.push([ item ]);
} else {
list[list.length - 1].push(item);

// `at` still not supported by safari.
// list.at(-1).push(item);
}
return list;
}, []);
}
// ... OP ... I have an array with names
const players = ['name1', 'name2', 'name3', 'name4', 'name5', 'name6', 'name7', 'name8'];
// ... OP ... I want to make teams of 2 people (randomly chosen)
//            and make new arrays from the results -> team
const teams = createListOfChunkLists(
shuffleArray([...players]), 2
);
console.log({ teams, players });
console.log({
teams: createListOfChunkLists(
shuffleArray([...players]), 2
),
players
});
console.log({
teams: createListOfChunkLists(
shuffleArray([...players]), 4
),
players
});
.as-console-wrapper { min-height: 100%!important; top: 0; }

最新更新