Lodash _.isMatch() method
What you’ll learn
- How
_.isMatch(object, source)performs partial deep matching. - How nested objects and arrays are evaluated in source-driven checks.
- Why missing required keys fail even when many other keys match.
- When to use
_.isMatchversus_.isEqualin validation logic.
Prerequisites
You understand plain objects and basic nested JSON-like data structures.
- You know the difference between subset checks and full equality checks.
- Try-it labs load lodash from the CDN.
Overview
Use _.isMatch for feature flags, request payload validation, filter predicates, and rule engines where source describes required fields while allowing additional object data.
Partial matching
Only keys in source are required for success.
Deep by default
Nested objects and arrays are compared recursively.
Missing keys fail
If source demands a field absent in object, result is false.
Syntax
_.isMatch(object, source) - object: value to inspect.
- source: required property/value subset to compare against.
- Returns:
trueif object contains equivalent source properties, otherwisefalse.
Basic subset match
Extra properties in the object do not hurt a successful subset comparison.
import isMatch from "lodash/isMatch";
var object = { a: 1, b: 2, c: 3 };
console.log(
"matchB2: " + isMatch(object, { b: 2 }) + "\n" + // true
"matchB1: " + isMatch(object, { b: 1 }) // false
); Nested objects and arrays
Source can target deep structures, including array content.
import isMatch from "lodash/isMatch";
var object = { profile: { role: "admin", active: true }, tags: ["a", "b"] };
console.log(
"nestedRole: " + isMatch(object, { profile: { role: "admin" } }) + "\n" + // true
"tagPrefix: " + isMatch(object, { tags: ["a"] }) // true
); Missing required properties
If source asks for unavailable properties, the match fails.
import isMatch from "lodash/isMatch";
var object = { id: 100, status: "ok" };
console.log(
"needsStatus: " + isMatch(object, { status: "ok" }) + "\n" + // true
"needsType: " + isMatch(object, { type: "service" }) // false
); 📋 _.isMatch vs related checks
| API | Behavior |
|---|---|
_.isMatch(object, source) | Partial deep comparison based on source keys. |
_.isEqual(a, b) | Full deep equality; both structures must fully match. |
_.matches(source) | Creates a reusable predicate equivalent to partially applying _.isMatch. |
=== | Strict reference/value equality only; no deep object comparison. |
Pitfalls to avoid
Unexpected true
If source is too small, many objects may match. Include enough required keys to avoid false positives.
Order matters
Array comparison is structural; position changes can affect results.
Type assumptions
Combine with explicit shape checks when input may be nullish or not object-like.
❓ FAQ
Summary
- Purpose: verify whether an object contains required source properties.
- Remember:
_.isMatchchecks subsets, not complete equality. - Next: explore more on Lodash _.isMatchWith().
_.isMatch(object, source) is a partial deep comparison: only properties present in source must match, so extra keys in object are ignored.
6 people found this page helpful
