You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
671 B

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