Lodash _.at() method
What you’ll learn
- How
_.at(object, [paths])plucks several values in one call and returns them as an array. - Three path styles: dotted string, array-of-keys, and rest arguments—and when each matters.
- When to reach for
_.atvs _.get() (single path) or _.pick() (sub-object). - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Familiar with dotted/array paths in Lodash (the shared model used by _.get, _.set, and friends).
- Nested objects: walking shapes like
{ user: { address: { city } } }via"user.address.city". - Array destructuring: downstream code often unpacks the result as
const [name, city, email] = _.at(...).
Overview
_.at is a multi-_.get: hand it an object and any number of paths, and it returns the values as an array in the same order. The result is always an array—perfect for positional destructuring.
Pluck many at once
Avoid a chain of _.get calls when you need three or four fields out of a record.
Order preserved
Slot N in the result is the value at path N—missing paths still take their slot as undefined.
Array path escape hatch
When a key contains a dot, pass it as an array path—[["weird.key"]]—so Lodash doesn’t split on it.
Syntax
_.at(object, [paths])
// paths can be a single string, an array of strings/paths, or several rest args. - object: source object (or array) to read from.
- paths: one or more paths. Each path is a dotted string (
"user.email") or an array of keys (["user", "email"]). - Returns: an array of values, one per input path, in input order. Missing paths produce
undefined.
Pluck a few fields out of a record
Three paths in, three values out, in the same order. Great for positional destructuring.
import at from "lodash/at";
const user = {
id: 1,
name: "John Doe",
address: { city: "New York", zip: "10001" },
email: "john@example.com"
};
const [name, city, email] = at(user, ["name", "address.city", "email"]);
// name -> "John Doe"
// city -> "New York"
// email -> "john@example.com" Missing paths keep their slot
The result array always has one entry per requested path; absent values come back as undefined so indexes stay aligned with your input.
import at from "lodash/at";
const user = { name: "John", address: { city: "NY" } };
at(user, ["name", "address.country", "phone"]);
// => [ "John", undefined, undefined ]
// ^^^^^^ ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^
// hit missing nested missing top-level Array paths for tricky keys & array indexes
When a key literally contains a dot, hand _.at an array path so it doesn’t split. Numeric segments also work on arrays.
import at from "lodash/at";
const data = {
"my.weird.key": "got it",
tags: ["alpha", "beta", "gamma"]
};
at(data, [["my.weird.key"], "tags[0]", "tags.2"]);
// => [ "got it", "alpha", "gamma" ]
// ^^^^^^^^ ^^^^^^^ ^^^^^^^
// array path bracket dotted index 📋 _.at vs _.get vs _.pick
| Topic | _.at | _.get | _.pick |
|---|---|---|---|
| Paths per call | One or many | One | One or many (keys/paths) |
| Return shape | Array of values | A single value | A new object |
| Missing path | undefined slot | defaultValue if provided, else undefined | Key simply omitted |
| Typical use | Positional destructure of many fields | One safe deep read with a default | Filter to a sub-object DTO |
Reach for _.at when downstream code wants positional access; pick _.pick when it wants the keys preserved.
Pitfalls to avoid
Missing paths surface as undefined
_.at has no equivalent of _.get’s third argument. Map the result yourself if you need fallbacks per path.
Accidental key splitting
Dotted strings split on every dot. Wrap literal dotted keys in an array path so Lodash treats them as a single segment.
Confusing with _.pick
_.at always returns an array; _.pick returns an object. Pick the API that matches the consumer’s shape expectation.
❓ FAQ
Summary
- Purpose: pluck one or more values from an object by path; result is always an array in path order.
- Remember: missing paths become
undefinedslots; wrap dotted-name keys in array paths; no per-path default. - Next: Lodash _.create(), _.get(), or the official Lodash docs for _.at.
_.at always returns an array, even for a single path—the result’s position lines up with the path’s position. To address a key that literally contains dots, wrap it in an array path: _.at(obj, [["weird.key"]]).
6 people found this page helpful
