Skip to content

sampleSize

js
_.sampleSize(collection, [n=1])

collectioncollection 的大小,获取唯一键处的 n 个随机元素。

¥Gets n random elements at unique keys from collection up to the size of collection.

新增于

¥Since

4.0.0

参数

¥Arguments

  1. collection (数组|对象):要采样的集合。

    ¥collection (Array|Object): The collection to sample.

  2. [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]

Lodash v4.17 中文网 - 粤ICP备13048890号