【发布时间】:2020-04-07 18:20:06
【问题描述】:
在 javascript 中,我想创建一个包含 20 个对象的数组,其中包含 2 个介于 1 和 250 之间的随机数。数组中的所有数字都希望彼此唯一。基本上是这样的:
const matches = [
{ player1: 1, player2: 2 },
{ player1: 3, player2: 4 },
{ player1: 5, player2: 6 },
{ player1: 7, player2: 8 },
...
]
// all unique numbers
我找到了这个其他方法
const indexes = [];
while (indexes.length <= 8) {
const index = Math.floor(Math.random() * 249) + 1;
if (indexes.indexOf(index) === -1) indexes.push(index);
}
但这只会返回一个数字数组:
[1, 2, 3, 4, 5, 6, 7, 8, ...]
【问题讨论】:
-
您好 dubs_tep,如果此页面上的答案解决了您的问题,请考虑将其标记为已接受。谢谢
标签: javascript arrays object unique