如何随机排列数组中的对象
How to shuffle objects in an array
我目前在javascript上遇到了问题。 请帮助我如何在数组中洗牌。 我真的很感激任何帮助。非常感谢!
// ______________________________________________Function to declare our var -> deck of cards
function getDeck() {
var deck = [
{suit: "H", face: "A"},
{suit: "H", face: "2"},
{suit: "H", face: "3"},
{suit: "H", face: "4"},
{suit: "H", face: "5"},
{suit: "H", face: "6"},
{suit: "H", face: "7"},
{suit: "H", face: "8"},
{suit: "H", face: "9"},
{suit: "H", face: "10"},
{suit: "H", face: "J"},
{suit: "H", face: "Q"},
{suit: "H", face: "K"},
{suit: "C", face: "A"},
{suit: "C", face: "2"},
{suit: "C", face: "3"},
{suit: "C", face: "4"},
{suit: "C", face: "5"},
{suit: "C", face: "6"},
{suit: "C", face: "7"},
{suit: "C", face: "8"},
{suit: "C", face: "9"},
{suit: "C", face: "10"},
{suit: "C", face: "J"},
{suit: "C", face: "Q"},
{suit: "C", face: "K"},
{suit: "D", face: "A"},
{suit: "D", face: "2"},
{suit: "D", face: "3"},
{suit: "D", face: "4"},
{suit: "D", face: "5"},
{suit: "D", face: "6"},
{suit: "D", face: "7"},
{suit: "D", face: "8"},
{suit: "D", face: "9"},
{suit: "D", face: "10"},
{suit: "D", face: "J"},
{suit: "D", face: "Q"},
{suit: "D", face: "K"},
{suit: "S", face: "A"},
{suit: "S", face: "2"},
{suit: "S", face: "3"},
{suit: "S", face: "4"},
{suit: "S", face: "5"},
{suit: "S", face: "6"},
{suit: "S", face: "7"},
{suit: "S", face: "8"},
{suit: "S", face: "9"},
{suit: "S", face: "10"},
{suit: "S", face: "J"},
{suit: "S", face: "Q"},
{suit: "S", face: "K"},
];
return deck;
}
这是随机播放代码。 我不知道这是否正确,所以也请检查。谢谢!
// ______________________________________________Function to shuffle cards
function shuffle(o) { // o is passed-in array
for (var j, x, i = o.length; i; j = parseInt(Math.random() * i, 10), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
// ______________________________________________Function to retrieve our deck of cards & save into localStorage
function getShoe(decks) {
var shoe = [];
var deck = getDeck();
this.decks = decks;
for (z=1;z<=decks;z++) {
shoe.push(deck);
}
shoe = shuffle(shoe);
return shoe;
localStorage.setItem('shoe', JSON.stringify(shoe));
}
你在这里做的是:
var shoe = []; // create an empty array
var deck = getDeck(); // create a SINGLE deck
this.decks = decks;
for (z=1;z<=decks;z++) {
shoe.push(deck); // insert the SAME deck many times into the array
}
shoe = shuffle(shoe); // you shuffle the array that contains the same deck many times, not the decks
return shoe;
你可以做:
var shoe = [];
for (var i=0; i<decks; i++){
shoe.push(shuffle(getDeck()));
}
return shoe;
相关文章:
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 在Javascript中转换对象数组
- 在JavaScript中通过索引从对象数组中获取值
- Backbone虹吸以获取对象数组
- 如何在DataTables 2.1中迭代对象数组
- Javascript-根据赋值顺序,按键合并对象数组
- 将事件附加到对象/数组
- jQuery$.inArray()总是返回-1和一个对象数组
- javascript处理一个对象数组以获得一个新的对象数组
- javascript在数组中获取对象数组中键的所有不同值
- 在对象数组中查找多个值的d3范围
- Undercore.js获取对象数组中键对象的值
- 在mongoose中使用正则表达式在对象数组中进行查询搜索
- 如何通过json对象数组为嵌套对象赋值
- 如何循环通过2个对象数组并通过匹配id进行合并
- 为对象数组创建列表项
- 如何使用javascript合并两个对象数组
- JSON到对象数组,并向每个对象添加项
- JavaScript:从对象数组中获取唯一值及其计数
- 按不同项目对对象数组进行排序