确保索引0之后的数组唯一性

时间:2019-03-25 10:24:12

标签: javascript ecmascript-6

给出以下代码:

random: function() {
  return Math.floor(Math.random() * 100 + 1)
}

let result = []

result.push(random()) // it doesn't matter if this integer is pushed again in the next line
[...Array(10)].forEach((_, i) => result.push(random())) // all of these have to be unique

如何确保没有重复项被推入result (可能是第一个),而却将数组的长度保持在11?

>

0 个答案:

没有答案