Lodash _.toPath() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Util

Definition

Converts a value into a property path array — the key segments Lodash uses for nested access. Part of Lodash Util since 4.0.0 (stable — not deprecated).

Note: It parses paths only. It does not read or write object values — pair it with _.get or _.set for that.

Syntax

Call _.toPath. Start with this line.

Lodash
_.toPath(value)
CallWhat it does
_.toPath(value)Primary signature.
_.toPath(...)Call the util helper.

Prefer import toPath from "lodash/toPath" when you want a smaller bundle.

Library declarations
Lodash
_.toPath(value)

Parameters

ParameterDescription
valueRequired. Path string, key array, or other value to convert

Return Value

Return ValueDescription
*New array of path keys (strings)

Lodash Examples

Example 1 — Official: Dot Path

Split a simple dotted path into keys.

Lodash
_.toPath('a.b.c');
// → ['a', 'b', 'c']
Try It Yourself

How It Works

  1. Input. The string "a.b.c" is a Lodash-style path.
  2. Parse. Dots mark key boundaries.
  3. Keys. Three segments: a, b, c.
  4. Result. A new array you can walk or pass to other helpers.

Example 2 — Official: Bracket Index

Array indexes in brackets become their own string keys.

Lodash
_.toPath('a[0].b.c');
// → ['a', '0', 'b', 'c']
Try It Yourself

How It Works

  1. Mix. Path uses both dots and [0].
  2. Index. [0] becomes the key "0" (a string).
  3. Rest. .b and .c append more keys.
  4. Order. Left-to-right order matches nested access depth.

Example 3 — Array Input

Already have keys? _.toPath returns a new shallow copy.

Lodash
var keys = ['a', 'b', 'c'];
var path = _.toPath(keys);

console.log(path);
// → ['a', 'b', 'c']

console.log(path === keys);
// → false
Try It Yourself

How It Works

  1. Detect. Input is already an array of keys.
  2. Copy. Lodash builds a new array (does not reuse the same reference).
  3. Normalize. Each segment is treated as a path key.
  4. Use. One API whether callers pass strings or arrays.

Example 4 — Why Not split('.')?

Native split keeps brackets glued to the key name.

Lodash
var path = 'items[0].name';

console.log(path.split('.'));
// → ['items[0]', 'name']

console.log(_.toPath(path));
// → ['items', '0', 'name']
Try It Yourself

How It Works

  1. split. Only splits on dots — items[0] stays one token.
  2. toPath. Understands bracket indexes as separate keys.
  3. Wrong key. "items[0]" is not a real property name on most objects.
  4. Prefer. Use _.toPath (or _.get) for mixed path syntax.

Example 5 — Path Array with _.get

Parse once, then read nested data with the key array.

Lodash
var obj = { a: [{ b: { c: 3 } }] };
var path = _.toPath('a[0].b.c');

_.get(obj, path);
// → 3

_.get(obj, 'a[0].b.c');
// → 3  (string path also works)
Try It Yourself

How It Works

  1. Parse. _.toPath yields ['a','0','b','c'].
  2. Get. _.get(obj, path) walks those keys.
  3. Value. Finds 3 at the nested location.
  4. Shortcut. Passing the string to _.get skips an explicit toPath call.

Browser & Runtime Support

_.toPath is a Lodash Util helper since 4.0.0. It runs anywhere Lodash 4.x runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.0.0+

_.toPath()

Available in [email protected] full builds and the lodash/toPath modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.0+
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.toPath Lodash 4.0.0+

Bottom line: Safe for production once Lodash 4+ is loaded. Prefer import toPath from "lodash/toPath" for smaller bundles. Use it when you need an explicit key array from a path string.

Frequently Asked Questions

_.toPath(value) converts a property path into an array of keys. Official: _.toPath('a.b.c') → ['a','b','c'] and _.toPath('a[0].b.c') → ['a','0','b','c'].
No. It only parses the path. Use _.get, _.set, or _.has to access or update nested data.
split('.') ignores brackets. 'items[0].name'.split('.') yields ['items[0]','name'] — one wrong key. _.toPath splits dots and brackets correctly.
Usually no. _.get(obj, 'a.b.c') accepts a string path. Call _.toPath when you need the key array for custom logic, logging, or APIs that expect an array.
Lodash returns a new shallow copy (keys normalized). Handy when callers may pass either a string path or a pre-built key array.
No. It parses common path syntax. Allow-list or validate paths from user input before _.get or _.set.

Next: _.uniqueId()

Generate unique ids, optionally with a prefix.

_.uniqueId() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful