pullAt.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var baseAt = require('../internal/baseAt'),
  2. baseCompareAscending = require('../internal/baseCompareAscending'),
  3. baseFlatten = require('../internal/baseFlatten'),
  4. basePullAt = require('../internal/basePullAt'),
  5. restParam = require('../function/restParam');
  6. /**
  7. * Removes elements from `array` corresponding to the given indexes and returns
  8. * an array of the removed elements. Indexes may be specified as an array of
  9. * indexes or as individual arguments.
  10. *
  11. * **Note:** Unlike `_.at`, this method mutates `array`.
  12. *
  13. * @static
  14. * @memberOf _
  15. * @category Array
  16. * @param {Array} array The array to modify.
  17. * @param {...(number|number[])} [indexes] The indexes of elements to remove,
  18. * specified as individual indexes or arrays of indexes.
  19. * @returns {Array} Returns the new array of removed elements.
  20. * @example
  21. *
  22. * var array = [5, 10, 15, 20];
  23. * var evens = _.pullAt(array, 1, 3);
  24. *
  25. * console.log(array);
  26. * // => [5, 15]
  27. *
  28. * console.log(evens);
  29. * // => [10, 20]
  30. */
  31. var pullAt = restParam(function(array, indexes) {
  32. indexes = baseFlatten(indexes);
  33. var result = baseAt(array, indexes);
  34. basePullAt(array, indexes.sort(baseCompareAscending));
  35. return result;
  36. });
  37. module.exports = pullAt;