pullAt.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import arrayMap from './_arrayMap.js';
  2. import baseAt from './_baseAt.js';
  3. import basePullAt from './_basePullAt.js';
  4. import compareAscending from './_compareAscending.js';
  5. import flatRest from './_flatRest.js';
  6. import isIndex from './_isIndex.js';
  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. export default pullAt;