简体   繁体   English

Javascript:尝试将项目从一个数组随机移动到另一个数组

[英]Javascript: Trying to randomly move items from one array to another

I've been stuck for a couple hours now trying to randomly select one item from one array (players) to another (team1). 我一直被困了两个小时,试图从一个数组(玩家)中随机选择一个项目到另一个(team1)中。

I've got it working by doing something else with splice, but unfortunately splice creates an array itself with the removed items and so I ended up getting an array with an array. 我通过对拼接执行其他操作来使其工作,但是不幸的是,拼接本身会使用删除的项目创建一个数组,因此最终获得了一个带有数组的数组。

This is what I got so far: 这是我到目前为止所得到的:

var players = ["P1", "P2", "P3", "P4"];

var team1 = [];
var team2 = [];

var select = Math.floor(Math.random() * players.length);
var tmp;

if (team1.length < 2) {
  tmp.push(players.splice(select, 1));
  team1.push(tmp.pop);
}

console.log(team1);
console.log(tmp);
console.log(players);

If I'm doing this all wrong I'm sorry, still pretty new to this website, help is appreciated. 如果我做错了所有错,对不起,对于该网站还很陌生,我们感谢您的帮助。

you try like this in your scenario 您在您的情况下尝试这样

var players = ["P1", "P2", "P3", "P4"];

var team1 = [];
var team2 = [];
var temp = players.slice();

for(i=0; i<temp.length; i++){
     var select = Math.floor(Math.random() * temp.length);
     console.log(select);
     if (team1.length <= temp.length/2) {
        team1.push(temp[select]);
      }
      temp.splice(select, 1);
}
team2 = temp.slice();

console.log('team 1 ---',team1);
console.log('team 2 ---',team2);
console.log('players ---', players);

You just have to select the first element from the array while splicing and pushing to team, 您只需在拼接并推入团队时从数组中选择第一个元素,

var players = ["P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8"];

var team1 = [];
var team2 = [];

var tmp = [];
while (team1.length < 4) {
  tmp.push(players.splice(Math.floor(Math.random() * players.length - 1), 1)[0]);
  team1.push(tmp.pop());
}

while (team2.length < 4) {
  tmp.push(players.splice(Math.floor(Math.random() * players.length - 1), 1)[0]);
  team2.push(tmp.pop());
}

console.log(team1);
console.log(team2);
console.log(tmp);
console.log(players);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Javascript 数组中从 2 个或多个 item 中随机拼接一个 item - Splice one item from 2 or more items randomly in Javascript array (JavaScript和HTML)将项目从一个列表移动到另一个列表 - (JavaScript & HTML )Move items from one list to another 使用Javascript从数组中随机选择唯一项 - Randomly selecting unique items from an array with Javascript 如何使用 hash 将元素从一个数组伪随机移动到另一个数组? - How can I move elements from one array to another pseudo-randomly using a hash? Javascript:将对象从一个数组移动到另一个数组:最佳方法? - Javascript: move objects from one array to another: Best approach? 用JavaScript将项目从一个数组交换到另一个数组的优雅方法? - Elegant way to swap items from one array to another in JavaScript? 在Javascript中将随机项目从一个数组移动到另一个数组 - Moving random items from one array to another in Javascript 如何有条件地将只有数字的项目从一个数组移动到另一个数组 - How to conditionally move only items with numbers from one array to another array jquery 如果条件(JavaScript ES6),将一个特定的 object 道具从一个数组移动到另一个对象数组 - move one specific object prop from one array to another array of objects, if condition (JavaScript ES6) 如何从javascript中的数组中随机选择多个项目进行替换 - How to select randomly multiple items, with replacement, from an array in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM