Lodash _.intersection() method
What you’ll learn
- How
_.intersection(array, ...arrays)builds the list of values present in every supplied array. - Why order and uniqueness follow the first array, and how SameValueZero affects primitives like
NaN. - When to reach for _.difference() instead (subtract sets), or
_.intersectionBy/_.intersectionWithwhen equality is not reference or SameValueZero. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Optional reads _.initial() and _.difference(); you should be comfortable with the idea that plain objects compare by reference unless you add extra logic.
- You understand that “in all arrays” means every argument must contain that value at least once.
- You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.intersection answers “what do these lists agree on?” Think permission lists, tag filters, or comparing IDs returned from two services—anything where membership must hold across every source.
All must match
A value survives only if it appears at least once in each argument array.
First wins order
The output is unique values ordered by their first occurrence in the first array.
New array
Inputs are read-only; you always get a fresh array reference back.
Syntax
_.intersection(array, ...arrays) - arrays: two or more arrays (or array-like values). Each value must exist in every argument to appear in the result.
- Returns: a new array of unique shared values, ordered by first appearance in the first argument.
Two lists
Only values present in both arrays are returned, in the order they first appear in the first array.
import intersection from "lodash/intersection";
intersection([2, 1], [2, 3]);
// [2] Three lists
Pass more arrays to tighten the filter: every extra argument is another constraint.
import intersection from "lodash/intersection";
intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
// [2, 1] NaN and object identity
SameValueZero finds NaN across arrays; ordinary objects still require the same reference in each list.
import intersection from "lodash/intersection";
intersection([NaN, 1], [NaN, 2]);
// [NaN] — SameValueZero treats NaN as equal to NaN.
const o = { x: 1 };
intersection([o, 3], [o, 4]);
// [o] — only the same object reference counts in every array. 📋 _.intersection vs _.difference
| API | Question it answers | Note |
|---|---|---|
_.intersection(a, b, …) | What appears in every list? | Result is a subset of the first array’s values (re-ordered uniquely). |
_.difference(a, b, …) | What is in the first list but not in the others? | Flattens later arguments one level into one exclusion pool. |
Pitfalls to avoid
No overlap
If nothing is shared, you get []. Double-check types (string "1" versus number 1) and reference equality for objects.
Duplicates collapse
The result is a set of unique values; repeated slots in the first array still produce at most one output entry.
No deep equality
Two different object literals with the same shape are not equal; use intersectionBy with a key or intersectionWith with a comparator when you need more than reference match.
❓ FAQ
Summary
- Purpose:
_.intersection()returns values that occur in every supplied array, using SameValueZero. - Order: uniqueness and ordering follow the first array.
- Next: Lodash _.intersectionBy(), _.initial() (earlier in this track), or the array methods hub.
When equality should run on a projected key (for example "userId"), use _.intersectionBy from Lodash; for arbitrary comparators, _.intersectionWith.
6 people found this page helpful
