如何进行对象引用?

时间:2017-11-22 20:22:57

标签: jquery math random

var $playerChoice1 = Math.floor((Math.random()*5+1));
var $playerChoice2 = Math.floor((Math.random()*(10-6+1)+6));

var $playmaking1 = $player+$playerChoice1.playmaking;
var $playmaking2 = $player+$playerChoice2.playmaking;
var $rebounding1 = $player+$playerChoice1.rebounding;
var $rebounding2 = $player+$playerChoice2.rebounding;

我想从随机选择的$ player对象中获取值($ player1,$ player2,..,$ player9,$ player10)。第一个玩家从1到5中选择,另一个从6到10中选择。我的代码不起作用(当然)。我应该如何处理它?<​​/ p>

1 个答案:

答案 0 :(得分:0)

var players = [...]; // ten players

var $playerChoice1 = Math.floor((Math.random() * 5));     // range [0, 4]
var $playerChoice2 = Math.floor((Math.random() * 5 + 5)); // range [5, 9]

var $playmaking1 = players[$playerChoice1].playmaking;
var $playmaking2 = players[$playerChoice2].playmaking;