nth.js 671 B

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