Lodash _.pull() method
What you’ll learn
- How
_.pull(array, ...values)deletes all matching elements from the existing array. - That the method mutates the array and returns the same reference (unlike filter-based copies).
- How SameValueZero affects primitives such as
NaNand how object identity affects removals. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Overview
_.pull is the variadic “strip these exact values out” helper. It walks the array, drops every slot that matches any argument (SameValueZero), compacts the tail, and returns the mutated array so you can chain or reassign confidently.
In place
The original binding changes length and contents; no new array is allocated for the removal pass.
All matches
Every duplicate of each supplied value disappears, not only the first hit.
Variadic values
List values as additional arguments; use pullAll when you already hold an array of values to remove.
Syntax
_.pull(array, [values...]) - array: collection to mutate (array-like values are coerced).
- values: any number of extra arguments; each unique value is removed everywhere it appears.
- Returns: the same
arrayreference after removal.
Remove primitives
Pass the values to strip; the array shrinks in place and the returned reference equals the input binding.
import pull from "lodash/pull";
const nums = [1, 2, 3, 1, 2];
pull(nums, 1, 2);
// nums is now [3] Remove NaN
SameValueZero treats NaN as equal to itself, so pull can clear noisy placeholders that indexOf historically struggled with.
import pull from "lodash/pull";
const readings = [NaN, 1, NaN];
pull(readings, NaN);
// readings is now [1] Object identity
Objects match only by reference; a fresh literal is never equal to a different literal, even with identical fields.
import pull from "lodash/pull";
const marker = { id: 1 };
const rows = [marker, { id: 1 }, marker];
pull(rows, marker);
// rows is now [{ id: 1 }] 📋 _.pull vs _.pullAll vs immutable filtering
| API | Input style | Note |
|---|---|---|
_.pull(array, a, b, c) | Variadic values | Mutates array; removes every match for each argument |
_.pullAll(array, [a, b, c]) | Array of values | Same mutation semantics when the list already lives in an array |
array.filter(x => !values.has(x)) | Predicate / set | Returns a new array; keeps the original untouched |
Pitfalls to avoid
Hidden aliasing
If another module holds the same array reference, your pull changes its data too—clone first when isolation matters.
Expecting a new array
Use slice(), spread, or filter when you must preserve the old snapshot for comparison or undo flows.
No deep matching
Nested objects are not compared by structure; only references and primitive SameValueZero rules apply.
❓ FAQ
Summary
- Purpose:
_.pull(array, ...values)removes every occurrence of each supplied value using SameValueZero and returns the mutated array. - Mutation: prefer
filter/slicepatterns when callers must keep the previous contents. - Next: Lodash _.pullAll(), _.nth() (earlier in this track), or the array methods hub.
Like _.fill(), _.pull mutates the array and returns the same reference so callers can keep using the original binding or chain further Lodash calls.
6 people found this page helpful
