pullAt.js 1.2 KB

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