Lodash pullAt is an array utility function that removes elements from an array at specified indexes and returns the removed elements. Unlike _.at, this method mutates the original array. It handles indexes flexibly, accepting them individually or as arrays.
npm install lodashimport _ from "lodash";For modular usage:
import pullAt from "lodash.pullat";CommonJS:
const _ = require("lodash");
// or
const pullAt = require("lodash.pullat");import _ from "lodash";
// Remove elements at specific indexes
const array = [5, 10, 15, 20];
const removed = _.pullAt(array, 1, 3);
console.log(array); // => [5, 15] (original array is mutated)
console.log(removed); // => [10, 20] (removed elements are returned)
// Using array of indexes
const data = ['a', 'b', 'c', 'd', 'e'];
const extracted = _.pullAt(data, [0, 2, 4]);
console.log(data); // => ['b', 'd'] (mutated)
console.log(extracted); // => ['a', 'c', 'e'] (extracted elements)Removes elements from array corresponding to indexes and returns an array of removed elements.
/**
* Removes elements from `array` corresponding to `indexes` and returns an
* array of removed elements.
*
* Note: Unlike `_.at`, this method mutates `array`.
*
* @param {Array} array The array to modify.
* @param {...(number|number[])} [indexes] The indexes of elements to remove,
* specified individually or in arrays.
* @returns {Array} Returns the new array of removed elements.
*/
function pullAt(array, ...indexes);Parameters:
array (Array): The array to modify...indexes (...(number|number[])): The indexes of elements to remove, specified individually or in arraysReturns:
Array: Returns the new array of removed elementsBehavior:
Usage Examples:
// Individual indexes
const arr1 = [1, 2, 3, 4, 5];
const removed1 = _.pullAt(arr1, 0, 2, 4);
// arr1: [2, 4]
// removed1: [1, 3, 5]
// Array of indexes
const arr2 = ['x', 'y', 'z', 'a', 'b'];
const removed2 = _.pullAt(arr2, [1, 3]);
// arr2: ['x', 'z', 'b']
// removed2: ['y', 'a']
// Mixed individual and array indexes
const arr3 = [10, 20, 30, 40, 50, 60];
const removed3 = _.pullAt(arr3, 0, [2, 4], 5);
// arr3: [20, 40]
// removed3: [10, 30, 50, 60]
// Empty array handling
const arr4 = [];
const removed4 = _.pullAt(arr4, 0, 1);
// arr4: []
// removed4: []
// Out of bounds indexes (safely ignored)
const arr5 = [1, 2, 3];
const removed5 = _.pullAt(arr5, 1, 10, 20);
// arr5: [1, 3]
// removed5: [2]When using the complete lodash library:
import _ from "lodash";
const result = _.pullAt(array, ...indexes);When using the standalone pullAt package:
// Install: npm install lodash.pullat
import pullAt from "lodash.pullat";
const result = pullAt(array, ...indexes);
// CommonJS
const pullAt = require("lodash.pullat");
const result = pullAt(array, ...indexes);<script src="https://cdn.jsdelivr.net/npm/lodash@4.6.0/lodash.min.js"></script>
<script>
const result = _.pullAt(array, ...indexes);
</script>These lodash functions provide similar array manipulation capabilities:
_.pull(array, ...values) - Removes specified values from array_.pullAll(array, values) - Removes array of values from array_.pullAllBy(array, values, iteratee) - Removes values using iteratee_.pullAllWith(array, values, comparator) - Removes values using comparator_.remove(array, predicate) - Removes elements matching predicate_.at(object, ...paths) - Gets values at specified paths (non-mutating)rest function to create variadic parameter handlingbaseFlatten and converted to stringsbaseAt to extract elements before removalbasePullAt for the actual array mutation with sorted indexes