Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 1 | var baseNth = require('./_baseNth'), |
| 2 | toInteger = require('./toInteger'); |
| 3 | |
| 4 | /** |
| 5 | * Gets the element at index `n` of `array`. If `n` is negative, the nth |
| 6 | * element from the end is returned. |
| 7 | * |
| 8 | * @static |
| 9 | * @memberOf _ |
| 10 | * @since 4.11.0 |
| 11 | * @category Array |
| 12 | * @param {Array} array The array to query. |
| 13 | * @param {number} [n=0] The index of the element to return. |
| 14 | * @returns {*} Returns the nth element of `array`. |
| 15 | * @example |
| 16 | * |
| 17 | * var array = ['a', 'b', 'c', 'd']; |
| 18 | * |
| 19 | * _.nth(array, 1); |
| 20 | * // => 'b' |
| 21 | * |
| 22 | * _.nth(array, -2); |
| 23 | * // => 'c'; |
| 24 | */ |
| 25 | function nth(array, n) { |
| 26 | return (array && array.length) ? baseNth(array, toInteger(n)) : undefined; |
| 27 | } |
| 28 | |
| 29 | module.exports = nth; |