Lodash _.without() method
What you’ll learn
- How
_.without(array, ...values)clones an array while stripping explicit blacklist entries. - Why SameValueZero matters for
NaN, signed zero, and primitive compares. - When _.difference() or _.pull() reads clearer than variadic
without. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Skim _.difference() for multi-array exclusion patterns—without compresses the common “omit these literals” case into a variadic tail.
- You understand that removing objects compares references unless you add iteratees elsewhere.
- You can open Try-it labs or run snippets locally.
Overview
_.without keeps auditing-friendly pipelines immutable—strip sentinel flags, demo accounts, or noisy primitives without rewriting consumers that still reference the untouched feed.
Blacklist literals
Pass every forbidden value as its own argument—no wrapping arrays.
Stable survivors
Earlier untouched elements keep their spacing relative to each other.
Immutable source
Original arrays remain observable for retries, logs, or Redux-style diffs.
Syntax
_.without(array, ...values) - array: source collection to filter.
- values: any number of exclusions compared with SameValueZero.
- Returns: new array omitting every matching occurrence.
Strip repeated primitives
Every argument is eligible for removal—duplicates in the source disappear per match without touching other numbers.
import without from "lodash/without";
without([2, 1, 2, 3], 1, 2);
// → [3] NaN matches under SameValueZero
Naive === scans miss NaN; Lodash removes every noisy sentinel consistently.
import without from "lodash/without";
without([NaN, 1, NaN, 2], NaN);
// → [1, 2] Objects respect reference identity
Pass the exact object reference you want stripped—structurally equal literals remain unless they share reference equality.
import without from "lodash/without";
const marker = { tag: "skip" };
without([marker, "keep", marker, { tag: "skip" }], marker);
// → ["keep", { tag: "skip" }] 📋 _.without vs difference, pull, reject
| API | Mutation | Best when |
|---|---|---|
_.without(array, ...values) | New array | Small literal blacklist at call site |
_.difference(array, ...arrays) | New array | Another collection already enumerates exclusions |
_.pull(array, ...values) | Mutates source | In-place cleanup with identical equality rules |
_.reject(collection, predicate) | New array | Removal logic is conditional rather than enumerative |
Pitfalls to avoid
Structural equality
without will not dedupe “look-alike” objects—project ids first or jump to differenceBy/differenceWith.
Accidental array wrapping
Pass scalars directly—wrapping values in an extra array makes Lodash hunt for that array object reference instead of its contents.
Huge blacklists
Very large exclusion lists scan repeatedly—consider converting exclusions to a Set with custom filtering when hotspots appear.
❓ FAQ
Summary
- Purpose:
_.without(array, ...values)returns a filtered copy excluding every listed value via SameValueZero. - Contrast: choose difference when exclusions arrive as collections, pull when mutation is acceptable.
- Next: Lodash _.xor(), _.unzipWith() (previous), or the array methods hub.
Think of without as the immutable twin of _.pull()—same blacklist mechanics, but your source array stays untouched for downstream readers.
6 people found this page helpful
