JAVASCRIPT   9

dropRight.js

Guest on 11th June 2021 03:45:48 AM

  1. var baseSlice = require('../internal/baseSlice'),
  2.     isIterateeCall = require('../internal/isIterateeCall');
  3.  
  4. /**
  5.  * Creates a slice of `array` with `n` elements dropped from the end.
  6.  *
  7.  * @static
  8.  * @memberOf _
  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 a callback for functions like `_.map`.
  13.  * @returns {Array} Returns the slice of `array`.
  14.  * @example
  15.  *
  16.  * _.dropRight([1, 2, 3]);
  17.  * // => [1, 2]
  18.  *
  19.  * _.dropRight([1, 2, 3], 2);
  20.  * // => [1]
  21.  *
  22.  * _.dropRight([1, 2, 3], 5);
  23.  * // => []
  24.  *
  25.  * _.dropRight([1, 2, 3], 0);
  26.  * // => [1, 2, 3]
  27.  */
  28. function dropRight(array, n, guard) {
  29.   var length = array ? array.length : 0;
  30.   if (!length) {
  31.     return [];
  32.   }
  33.   if (guard ? isIterateeCall(array, n, guard) : n == null) {
  34.     n = 1;
  35.   }
  36.   n = length - (+n || 0);
  37.   return baseSlice(array, 0, n < 0 ? 0 : n);
  38. }
  39.  
  40. module.exports = dropRight;

Raw Paste


Login or Register to edit or fork this paste. It's free.