mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 14:37:49 +00:00
19 lines
396 B
JavaScript
19 lines
396 B
JavaScript
/**
|
|
* Gets a random element from `array`.
|
|
*
|
|
* @since 2.0.0
|
|
* @category Array
|
|
* @param {Array} array The array to sample.
|
|
* @returns {*} Returns the random element.
|
|
* @example
|
|
*
|
|
* sample([1, 2, 3, 4])
|
|
* // => 2
|
|
*/
|
|
function sample(array) {
|
|
const length = array == null ? 0 : array.length
|
|
return length ? array[Math.floor(Math.random() * length)] : undefined
|
|
}
|
|
|
|
export default sample
|