pullAt.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var arrayMap = require('./_arrayMap'),
  2. baseAt = require('./_baseAt'),
  3. baseFlatten = require('./_baseFlatten'),
  4. basePullAt = require('./_basePullAt'),
  5. compareAscending = require('./_compareAscending'),
  6. isIndex = require('./_isIndex'),
  7. rest = require('./rest');
  8. /**
  9. * Removes elements from `array` corresponding to `indexes` and returns an
  10. * array of removed elements.
  11. *
  12. * **Note:** Unlike `_.at`, this method mutates `array`.
  13. *
  14. * @static
  15. * @memberOf _
  16. * @since 3.0.0
  17. * @category Array
  18. * @param {Array} array The array to modify.
  19. * @param {...(number|number[])} [indexes] The indexes of elements to remove.
  20. * @returns {Array} Returns the new array of removed elements.
  21. * @example
  22. *
  23. * var array = ['a', 'b', 'c', 'd'];
  24. * var pulled = _.pullAt(array, [1, 3]);
  25. *
  26. * console.log(array);
  27. * // => ['a', 'c']
  28. *
  29. * console.log(pulled);
  30. * // => ['b', 'd']
  31. */
  32. var pullAt = rest(function(array, indexes) {
  33. indexes = baseFlatten(indexes, 1);
  34. var length = array ? array.length : 0,
  35. result = baseAt(array, indexes);
  36. basePullAt(array, arrayMap(indexes, function(index) {
  37. return isIndex(index, length) ? +index : index;
  38. }).sort(compareAscending));
  39. return result;
  40. });
  41. module.exports = pullAt;