Lodash _.pullAll() method
What you’ll learn
- How
_.pullAll(array, values)removes every element fromarraythat appears in thevalueslist. - Why this mirrors _.pull() but accepts a single array argument instead of a variadic list.
- That the operation mutates
arrayand returns the same reference, with SameValueZero matching rules. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.pull() first for the same removal semantics with variadic arguments.
- You understand that mutating an array every variable points at affects every observer of that reference.
- You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.pullAll is the array-driven twin of _.pull. Instead of listing values as separate parameters, you pass one array of forbidden values—often loaded from configuration or user input—and Lodash removes every matching slot from the target collection.
Ban list array
Keep removable IDs or tokens in one array and pass it wholesale.
In place
The first argument is mutated; the return value is the same reference.
All matches
Every duplicate of each banned value is removed, not only the first hit.
Syntax
_.pullAll(array, values) - array: collection to mutate (array-like values are coerced).
- values: array of values to strip from
arraywherever they appear (SameValueZero). - Returns: the same
arrayreference after removal.
Remove with a value array
The second argument lists everything to delete; order in the ban list does not matter.
import pullAll from "lodash/pullAll";
const nums = [1, 2, 3, 1, 2, 3];
pullAll(nums, [1, 3]);
// nums is now [2, 2] Dynamic ban list
Reuse the same helper when the removable set is built at runtime—for example stripping sentinel values from telemetry.
import pullAll from "lodash/pullAll";
const scores = [10, 0, 5, null, 10];
const banned = [0, null];
pullAll(scores, banned);
// scores is now [10, 5, 10] Object identity
The values array can hold references; only slots strictly equal to those references are removed.
import pullAll from "lodash/pullAll";
const marker = { id: 1 };
const rows = [marker, { id: 1 }, marker];
pullAll(rows, [marker]);
// rows is now [{ id: 1 }] 📋 _.pullAll vs _.pull vs spread
| API | When to use | Note |
|---|---|---|
_.pullAll(array, values) | Ban list already in an array | No manual ... spread of the list |
_.pull(array, ...values) | Fixed handful of literals | Variadic call mirrors other Lodash APIs |
array.filter(x => !ban.has(x)) | Immutable copy | Leaves array unchanged; allocate when you must not mutate |
Pitfalls to avoid
Shared references
Any holder of the same array reference sees the mutation immediately—clone before pullAll when you need an undo buffer.
Wrong arity
pullAll expects the second argument to be the values array; accidentally passing bare primitives skips the intended removal list.
No deep equality
Objects still compare by reference; deep structures are not matched field-by-field.
❓ FAQ
Summary
- Purpose:
_.pullAll(array, values)removes every occurrence of each entry invaluesfromarrayusing SameValueZero. - Shape: prefer this over variadic
pullwhen the removable set is already stored as an array. - Next: Lodash _.pullAllBy(), _.pull() (earlier in this track), or the array methods hub.
When the ban list is already an array variable (for example from configuration), pullAll reads more cleanly than pull(array, ...list) with manual spreads.
6 people found this page helpful
