Lodash _.pullAt() method
What you’ll learn
- How
_.pullAt(array, indexes)deletes specific indexes and returns the removed values as a new array. - That the source
arrayis still mutated in place (length changes, survivors shift). - How this differs from
spliceergonomics and from value-based _.pull(). - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Helpful context: _.nth() for index thinking and _.pullAllWith() for comparator-based removals.
- You understand zero-based indexes and that removing an element shifts every higher index down by one.
- You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.pullAt is the surgical index remover: supply one or many indexes, Lodash strips those slots from the collection, and hands you a fresh array with exactly the removed elements. The original array keeps the survivors—just shorter.
By index
Target explicit positions instead of searching for values.
Removed chunk
Return value is the list of pulled-out elements, not the shortened array.
In place
The source array is mutated; clone first when you need a rollback copy.
Syntax
_.pullAt(array, [indexes]) - array: collection to mutate (array-like values are coerced).
- indexes: integers or nested arrays of integers; Lodash flattens extra argument layers.
- Returns: a new array containing the removed elements (not the mutated
arrayreference).
Multiple indexes
Pass an array of indexes; survivors compact while the return value collects the removed entries.
import pullAt from "lodash/pullAt";
const letters = ["a", "b", "c", "d"];
const removed = pullAt(letters, [1, 3]);
// removed is ["b", "d"]; letters is now ["a", "c"] Variadic indexes
Indexes can be listed as additional arguments; Lodash normalizes them the same as a single flat array.
import pullAt from "lodash/pullAt";
const nums = [10, 20, 30, 40, 50];
const removed = pullAt(nums, 0, 2, 4);
// removed is [10, 30, 50]; nums is now [20, 40] Single index
Removing one slot still returns an array of length one so call sites can always treat the result as a list.
import pullAt from "lodash/pullAt";
const queue = ["first", "second", "third"];
const removed = pullAt(queue, 1);
// removed is ["second"]; queue is now ["first", "third"] 📋 _.pullAt vs Array.prototype.splice vs _.pull
| API | Focus | Note |
|---|---|---|
_.pullAt(array, indexes) | Remove by index list | Returns removed values; mutates array |
splice(start, deleteCount, ...items) | Window delete / insert | Returns removed segment; can insert replacements at start |
_.pull(array, ...values) | Remove by value | Returns the mutated array reference, not a removed-values array |
Pitfalls to avoid
Shifting indexes
After a removal, later indexes refer to different elements; compute index lists on a snapshot or remove from the highest index downward in your own loops.
Return shape
Do not assign the return value to the variable that should still point at the full collection—it is only the removed slice.
Shared arrays
Other modules holding the same reference see the mutation immediately—clone first when isolation matters.
❓ FAQ
Summary
- Purpose:
_.pullAt(array, indexes)deletes the listed indexes and returns a new array of the removed elements. - Mutation:
arrayis shortened in place; capture the return value when you need the pulled values. - Next: Lodash _.remove(), _.pullAllWith() (previous), or the array methods hub.
Unlike _.pull(), which returns the same shortened array reference, _.pullAt returns a new array containing only the elements that were spliced out—handy when you need both the survivors and the removed chunk.
6 people found this page helpful
