nth.js 671 B

123456789101112131415161718192021222324252627282930
  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;