drop.js 890 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. var baseSlice = require('./_baseSlice'),
  2. toInteger = require('./toInteger');
  3. /**
  4. * Creates a slice of `array` with `n` elements dropped from the beginning.
  5. *
  6. * @static
  7. * @memberOf _
  8. * @since 0.5.0
  9. * @category Array
  10. * @param {Array} array The array to query.
  11. * @param {number} [n=1] The number of elements to drop.
  12. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  13. * @returns {Array} Returns the slice of `array`.
  14. * @example
  15. *
  16. * _.drop([1, 2, 3]);
  17. * // => [2, 3]
  18. *
  19. * _.drop([1, 2, 3], 2);
  20. * // => [3]
  21. *
  22. * _.drop([1, 2, 3], 5);
  23. * // => []
  24. *
  25. * _.drop([1, 2, 3], 0);
  26. * // => [1, 2, 3]
  27. */
  28. function drop(array, n, guard) {
  29. var length = array == null ? 0 : array.length;
  30. if (!length) {
  31. return [];
  32. }
  33. n = (guard || n === undefined) ? 1 : toInteger(n);
  34. return baseSlice(array, n < 0 ? 0 : n, length);
  35. }
  36. module.exports = drop;