Ramda’s R.path() answers a nested question: what value sits at this trail of keys? You pass a path array, then the object (or array). You get the nested value, or undefined if any step is missing. Native JavaScript’s cousin is optional chaining: obj?.a?.b. That is data-first. path is data-last, so R.path(['user', 'name']) is already a function of one object — ready for R.map or R.pipe.
The official sample is the whole idea. R.path(['a', 'b'], {a: {b: 2}}) is 2. The same path on {c: {b: 2}} is undefined. Integers mix in for arrays: ['a', 'b', 0] reads the first item of a.b. Need one key? That is R.prop. Need one list index? That is R.nth. Need to know if the path exists? That is R.hasPath. Need a default? Next is R.pathOr. Need to write the path? That is R.assocPath.
Syntax
Call R.path. Start with this line.
Ramda
R.path(path, obj)
Call
What it does
R.path(path, obj)
Primary signature.
R.path(...)
Call the object helper.
Prefer import { path } from "ramda" when you want a named import.
Library declarations
Ramda
R.path(path, obj)
Parameters
Parameter
Description
path
Required. Array. Keys to walk. Strings (and symbols) are object keys. Integers are array indexes, including negatives. An empty path returns obj itself.
obj
Required. Object or Array. The source. null and undefined yield undefined. The source is never mutated.
Return Value
Return Value
Description
Any or undefined
The value at the tail of path, or undefined if any node is missing, null, or not indexable. A stored undefined looks the same as a miss.
Ramda Examples
📚 Getting Started
The official nested sample, then the same helper on an array inside an object.
Example 1 — Official Nested Path
A present trail returns the value. A missing first key returns undefined without throwing.
path(['a'], obj) matches prop('a', obj). The list slot matches nth. hasPath still says b exists when its value is undefined. If you must tell a stored hole from a missing key, use R.hasPath, not path alone.
Example 4 — Negative Indexes and Integer Keys
Negatives count from the end of an array, through R.nth. An integer on a plain object still reads the coerced key. A negative on a non-list is unspecified leftover undefined.
-2 on [1, 2, 3] is the second-to-last item, 2. nth(2, {'2': 2}) still finds key '2' because JavaScript coerces the index. nth(-2, {'-2': 'a'}) does not look up the string '-2' — the leftover is undefined. Those three calls are the rest of the docs sample.
Example 5 — Curry, Map, Empty Path, Dotted String
Fill the path to map over rows. An empty path returns the whole value. A dotted string is one literal key, not two.
Because path is arity 2, filling the path leaves a function of one object. An empty array never walks, so you get the input back. ['user.name'] looks for a key whose name includes a dot. Split it: ['user', 'name']. Lodash _.get(obj, 'user.name') parses dots; Ramda does not.
Compatibility
Browser Support
R.path() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Ramda 0.2.0+ · Any JS runtime
Ramda.js R.path()
Load Ramda 0.2.0 or later via CDN or npm, then call R.path with a path array and an object. No extra browser API is required.
100%With Ramda 0.2.0+
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
R.path()Universal
Bottom line: Safe in any project that includes Ramda 0.2.0 or later. Use a CDN build such as ramda.min.js in the browser or install ramda from npm for Node. These labs use [email protected].
Frequently Asked Questions
R.path(path, obj) walks a list of keys and returns the nested value. A missing segment, null, or undefined along the way yields undefined. Added in v0.2.0. Strings are object keys. Integers are array indexes (including negatives via R.nth).
R.path(['a', 'b'], {a: {b: 2}}) is 2. R.path(['a', 'b'], {c: {b: 2}}) is undefined. R.path(['a', 'b', 0], {a: {b: [1, 2, 3]}}) is 1. R.path(['a', 'b', -2], {a: {b: [1, 2, 3]}}) is 2.
prop reads one object key. nth reads one list index. path walks a path array that can mix both. hasPath asks whether the path exists as own keys; path returns the value, so undefined can mean missing or a stored undefined.
No. A missing key, a non-object mid-path, null, or undefined returns undefined. That is safer than obj.a.b, which throws when a is missing.
Use R.pathOr(default, path, obj). path itself has no default. R.propOr is the one-key cousin.
No. R.path is a stable Ramda object helper from v0.2.0. It is not deprecated, experimental, or non-standard.