主题
sampleSize
js
_.sampleSize(collection, [n=1])从 collection 到 collection 的大小,获取唯一键处的 n 个随机元素。
¥Gets n random elements at unique keys from collection up to the size of collection.
新增于
¥Since
4.0.0
参数
¥Arguments
collection(数组|对象):要采样的集合。¥
collection(Array|Object): The collection to sample.[n=1](数值):要采样的元素数。¥
[n=1](number): The number of elements to sample.
返回
¥Returns
(数组):返回随机元素。
¥(Array): Returns the random elements.
示例
¥Example
js
_.sampleSize([1, 2, 3], 2);
// => [3, 1]
_.sampleSize([1, 2, 3], 4);
// => [2, 3, 1]