Lodash _.remove() method
What you’ll learn
- How
_.remove(array, predicate)deletes elements wherever the predicate returns a truthy value. - That the source array is mutated while Lodash returns an array of the removed values.
- When predicates beat value-based helpers such as _.pull().
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Comfort with _.findIndex()-style predicates helps; contrast with value removals in _.pull() and index removals in _.pullAt().
- You understand that shrinking an array shifts indexes for elements that remain.
- You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.remove is the in-place sibling of filtering logic: walk the array, splice out anything your predicate approves, and collect those evicted values into the returned array—ideal when you still need the rejects for logging, undo buffers, or analytics.
In place
The argument array loses matching entries immediately; plan clones when other owners still need the old snapshot.
Predicate driven
Express rules as functions instead of enumerating literals—great for ranges, nested fields, or dynamic criteria.
Removed chunk
The return value mirrors _.pullAt’s “what got extracted” pattern, but membership comes from predicate truthiness.
Syntax
_.remove(array, predicate) - array: collection to mutate (array-like values are coerced).
- predicate: invoked per element as
predicate(value, index, array); truthy results schedule removal. - Returns: a new array containing every removed element, ordered by removal.
Predicate on primitives
Strip even numbers; the leftover odds stay in nums while removed captures everything that failed the keep rule.
import remove from "lodash/remove";
const nums = [1, 2, 3, 4];
const removed = remove(nums, (n) => n % 2 === 0);
// nums is now [1, 3]; removed is [2, 4] Objects by field
Predicates shine when rows carry structured data—here everything with archived: true vanishes from the live list.
import remove from "lodash/remove";
const rows = [
{ id: 1, archived: false },
{ id: 2, archived: true },
{ id: 3, archived: false }
];
const archived = remove(rows, (r) => r.archived === true);
// rows keeps active records; archived holds the removed rows Using index
The second argument is the live index—handy for trimming tails without hard-coding splice arithmetic.
import remove from "lodash/remove";
const xs = ["a", "b", "c", "d"];
remove(xs, (_v, i, arr) => i >= arr.length - 2);
// xs is now ["a", "b"] — last two entries removed 📋 _.remove vs _.pull vs _.filter vs _.reject
| API | Mutation | Returns |
|---|---|---|
_.remove(array, pred) | Mutates array | Array of removed elements |
_.pull(array, ...values) | Mutates array | Same array reference after stripping literals |
_.filter(array, pred) | Does not mutate | New array of kept elements |
_.reject(array, pred) | Does not mutate | New array omitting predicate matches |
Pitfalls to avoid
Hidden aliasing
Other holders of the same array reference see the mutation instantly—clone first when isolation matters.
Side effects inside predicates
Keep predicates pure; pushing to the same array or relying on external counters mid-removal invites subtle bugs as indexes shift.
Expecting survivors
The returned array is the removed slice. Read array itself after the call when you need what stayed behind.
❓ FAQ
Summary
- Purpose:
_.remove(array, predicate)deletes predicate matches in place and returns those elements as a new array. - Immutable alternative: pair
filter/rejectwhen you must preserve the original ordering snapshot. - Next: Lodash _.reverse(), _.pullAt() (previous), or the array methods hub.
Unlike _.pull(), which matches explicit values with SameValueZero, _.remove deletes items whenever your predicate says so—similar to how _.pullAt() hands back the removed chunk, but driven by logic instead of indexes.
6 people found this page helpful
