Lodash Object methods
What you’ll learn
- What to know before you start (see Prerequisites).
- How the Object group maps to everyday tasks (safe reads, partial shapes, deep merge, iteration).
- When helpers mutate the destination (
assign,merge,set) versus returning new objects (pick,omit,mapValues). - How to import Lodash efficiently in modern bundlers.
- Where to open each
_.methodNametutorial on CodeToFun as those pages land.
Prerequisites
Plain objects, property dot/bracket notation, and the difference between own properties and the prototype chain. Optional Number methods when you clamp numeric fields before writing them into objects.
- Object literals: creating and reading
{ a: 1, b: { c: 2 } }, including nested paths. - Immutability mindset: knowing when your reducer or API layer expects a new reference versus an in-place update.
- First-class functions: iteratees and customizers used by
mapValues,mergeWith,pickBy, and similar APIs. - Modules:
importwith a bundler orrequirein Node.js so the install snippets match your setup.
Key concepts
Lodash reuses a small vocabulary across the Object category. These four ideas appear in signatures, docs, and TypeScript typings; recognizing them makes every method faster to read.
Path
Many helpers accept a path as a dotted string ("a.b.c"), an array of keys, or a deep path list—see get, set, has, unset.
Iteratee
A function or shorthand that projects values or keys before filtering or mapping—used by mapValues, mapKeys, pickBy, omitBy.
Own vs inherited
forOwn / keys stay on own enumerable string keys; forIn / keysIn also walk the prototype chain.
Destination mutation
assign, merge, set, update, and unset write into an existing object; pick/omit return fresh shells.
Overview
Lodash object helpers cover safe deep reads, whitelisting and blacklisting keys, recursive merging, prototype-aware iteration, and path-based mutation — with one consistent mental model for iteratees and customizers.
Read & probe
get, has, at, and result navigate optional data without long && chains.
Shape & copy
pick, omit, assign, defaults, and merge reshape objects for APIs and state snapshots.
Transform
mapValues, mapKeys, invert, and transform project collections without manual loops.
⚖️ Lodash vs native objects
Modern JavaScript adds Object.assign, spread copies, optional chaining, and structuredClone. Lodash still helps when you need deep paths, recursive merge rules, or iteratee shorthands that would otherwise sprawl.
| Situation | Prefer native | Consider Lodash |
|---|---|---|
| Shallow copy one level | { ...obj } or Object.assign({}, obj) | assign when you already import lodash nearby |
| Deep optional read | Optional chaining obj?.a?.b | get with array paths, defaults, or dynamic segments |
| Recursive merge | Hand-rolled recursion or libraries | merge / mergeWith with documented array behavior |
| Whitelist keys for an API DTO | Destructuring + literal | pick with many keys or path lists |
| Walk prototype chain | for...in + hasOwn | forIn, keysIn, hasIn with consistent semantics |
Install and import
Install the core package once per project, then import individual functions so bundlers can tree-shake unused helpers.
npm install lodash import get from "lodash/get";
import pick from "lodash/pick";
const user = { id: 42, name: "Ada", password: "secret" };
const safe = pick(user, ["id", "name"]);
const city = get(user, "address.city", "Unknown");
// safe: { id: 42, name: "Ada" } — password omitted
// city: "Unknown" — missing path uses default 🔄 Mutating vs new objects
Some Lodash object helpers write into the object you pass as the destination. Others return a new object and never touch the source. Mixing them up is a common Redux and React state bug.
| Style | Typical methods | Rule of thumb |
|---|---|---|
| Mutates destination | assign, merge, defaults, set, update, unset | Pass a draft object or clone first (cloneDeep) if consumers share the reference. |
| Returns new data | pick, omit, mapValues, mapKeys, invert | Safe to return from reducers or memoized selectors without touching the input. |
When you control the target (for example const next = {}; merge(next, a, b)), mutating helpers avoid allocating intermediate shells.
Suggested learning path
If you are new to Lodash objects, walk through this order in the REPL or a scratch file. Each step builds on the previous one.
- Safe reads:
get,has, andatfor defensive access. - Shape control:
pick/omitbefore sending data over the wire. - Copies & defaults:
assignanddefaultsfor shallow composition. - Deep merge:
mergewhen configs nest objects and arrays. - Path writes:
set/updatefor immutable-style helpers that still mutate the target you choose.
💻 Environment and versions
- Lodash 4.x: the method list on this page matches the stable 4.x Object exports from
lodash@^4on npm (includingentries/entriesInaliases). - Node.js and browsers: same package runs in both; choose ESM imports in bundlers and Vite, or
require('lodash/get')in CommonJS projects. - TypeScript: install
@types/lodashfor typings on namespace and per-method imports.
Method index
Each row links to a focused tutorial when it exists on this site. URLs follow the /lodash/object/{method-kebab} pattern (for example /lodash/object/merge-with).
| Method | What it does |
|---|---|
_.assign() | Copy own enumerable properties from sources onto the destination object (mutates). |
_.assignIn() | Like assign, but also copies inherited enumerable string keys from sources. |
_.assignInWith() | Like assignIn with a customizer for merged values. |
_.assignWith() | Like assign with a customizer for merged values. |
_.at() | Collect values at one or more property paths into an array. |
_.create() | Create a new object linking to the given prototype. |
_.defaults() | Fill undefined own properties on object from sources (shallow). |
_.defaultsDeep() | Fill undefined properties recursively from sources. |
_.entries() | Own enumerable string-keyed entries as pairs (alias of toPairs). |
_.entriesIn() | Own and inherited string-keyed entries as pairs (alias of toPairsIn). |
_.extend() | Alias of assignIn (copy including inherited enumerable keys). |
_.extendWith() | Alias of assignInWith. |
_.findKey() | First own key whose value satisfies the predicate. |
_.findLastKey() | Last own key whose value satisfies the predicate. |
_.forIn() | Invoke iteratee for each own and inherited enumerable string key. |
_.forInRight() | Like forIn but walks keys right-to-left. |
_.forOwn() | Invoke iteratee for each own enumerable string key. |
_.forOwnRight() | Like forOwn but walks keys right-to-left. |
_.functions() | Return names of own enumerable function properties. |
_.functionsIn() | Return names of own and inherited enumerable functions. |
_.get() | Safe deep get with optional default when path is missing. |
_.has() | Return true if path exists as own property (hasOwnProperty style). |
_.hasIn() | Return true if path exists anywhere in the prototype chain. |
_.invert() | Swap keys and values; collisions become arrays of keys. |
_.invertBy() | Like invert but values grouped by iteratee result. |
_.invoke() | Call a method at path with arguments on object. |
_.keys() | Own enumerable string keys of object. |
_.keysIn() | Own and inherited enumerable string keys. |
_.mapKeys() | Build a new object with keys mapped by iteratee. |
_.mapValues() | Build a new object with values mapped by iteratee. |
_.merge() | Deep merge sources into destination (mutates destination). |
_.mergeWith() | Like merge with customizer for clashes. |
_.omit() | Return new object without listed own paths. |
_.omitBy() | Return new object omitting keys where predicate is truthy. |
_.pick() | Return new object with only listed own paths. |
_.pickBy() | Return new object keeping keys where predicate is truthy. |
_.result() | Resolve path: functions are invoked, missing segments use default. |
_.set() | Set value at path, creating nested objects as needed (mutates). |
_.setWith() | Like set with customizer for creating nested containers. |
_.toPairs() | Own enumerable string-keyed entries as [key, value] pairs. |
_.toPairsIn() | Own and inherited enumerable string-keyed entries as pairs. |
_.transform() | Alternative to reduce for building or mutating accumulator object. |
_.unset() | Delete property at path (mutates). |
_.update() | Update value at path with updater function (mutates). |
_.updateWith() | Like update with customizer for creating parents. |
_.values() | Own enumerable string-keyed values. |
_.valuesIn() | Own and inherited enumerable string-keyed values. |
Pitfalls to avoid
Shared references
merge(state, patch) mutates state in place; Redux and React expect new roots when you mean to trigger updates.
Typos in strings
A wrong dotted path silently returns defaults from get or writes the wrong branch with set; prefer array paths for dynamic segments.
Depth surprise
assign overwrites nested objects wholesale; merge recursively combines plain objects. Pick the one that matches your payload shape.
❓ FAQ
Summary
- Scope: Lodash object helpers complement native static methods with paths, merge depth, and iteratee-driven projections.
- Bundles: import per method to keep client payloads small.
- Next step: open Lodash _.assign() or pick any row from the index table above.
Lodash extend and extendWith are aliases of assignIn and assignInWith—the older names for “copy enumerable keys including inherited ones” onto the destination.
9 people found this page helpful
