Ramda’s R.objOf() answers a tiny question: give me an object with this one key set to this one value. You pass the key, then the value. You get a new object. Nothing else is copied, because there is no source object. Native JavaScript’s cousin is a computed property: { [key]: val }. That is data-first and painful to reuse. objOf is data-last, so R.objOf('status') is already a function of one value — ready for R.map or R.compose.
The official sample is the whole idea. Map each string to {match_phrase: thatString}, then wrap the list as {must: [...]}. That is how query builders and API payloads often look. Need a two-item list instead? That is the docs sibling, R.pair. Need to set a key on an existing object? That is R.assoc. Need one item in an array? That is R.of.
Syntax
Call R.objOf. Start with this line.
Ramda
R.objOf(key, val)
Call
What it does
R.objOf(key, val)
Primary signature.
R.objOf(...)
Call the object helper.
Prefer import { objOf } from "ramda" when you want a named import.
Library declarations
Ramda
R.objOf(key, val)
Parameters
Parameter
Description
key
Required. String (or any property key). The single own property name on the new object. Numbers become string keys, as in ordinary JavaScript objects.
val
Required. Any. The value stored at that key. Nested objects and arrays are stored by reference, not cloned.
Return Value
Return Value
Description
Object
A new plain object with exactly one own enumerable property: key set to val.
Ramda Examples
📚 Getting Started
The official Ramda compose sample, then the one-key leftover and a curried wrapper.
Example 1 — Official matchPhrases Compose
Map each string to a one-key object, then wrap the whole list under must. That is the docs sample from ramdajs.com.
compose runs right-to-left. First, R.map(R.objOf('match_phrase')) turns each string into a tiny object. Then R.objOf('must') wraps that array. The leftover is a query-builder shape. That is the docs sample from ramdajs.com.
Example 2 — Simple Wrap and Curry
The everyday leftover is one key and one value. Fill the key to get a function of one value.
R.objOf('c', 3) is the smallest leftover. Because the helper is arity 2, R.objOf('status') waits for the value. Reuse that wrapper on as many strings as you like. A computed property would need a new arrow each time: v => ({ status: v }).
📈 Practical Patterns
Compare cousins, wrap every list item, then merge a wrapper onto an existing record.
Example 3 — vs pair, of, and assoc
Same two pieces of data, three different leftovers. assoc also needs a source object so sibling keys can survive.
objOf starts from nothing and writes one property. pair always returns a two-slot list (the docs sibling). of wraps one value as a list. assoc copies {keep: 1} and adds foo. If you already have an object, assoc (or merge) is the right cousin, not objOf.
Example 4 — Map a Wrapper Over Values
Fill the key, then map. Each number becomes a one-field record. Nested values stay shared by reference.
R.objOf('id') is a function of one value, so R.map can call it. The second snippet stores the same nested pointer inside the new object. Mutating wrapped.user.city would also change nested.city. Clone first if you need a deep copy.
Example 5 — Merge, Nested Wrap, Numeric Keys
Combine a wrapper with an existing record. Nest wrappers. Remember that a number key becomes a string property.
mergeRight copies the leftover of objOf onto an existing record. The nested call wraps names, then wraps the list. A numeric key is coerced the same way a literal {1: 'a'} is. Need several keys from two lists? That is R.zipObj, not repeated objOf.
Compatibility
Browser Support
R.objOf() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Ramda 0.18.0+ · Any JS runtime
Ramda.js R.objOf()
Load Ramda 0.18.0 or later via CDN or npm, then call R.objOf with a key and a value. No extra browser API is required.
100%With Ramda 0.18.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.objOf()Universal
Bottom line: Safe in any project that includes Ramda 0.18.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.objOf(key, val) returns a new object with exactly one own property: that key set to val. Added in v0.18.0. The source is a fresh {} plus one assignment.
pair returns a two-item array [key, val]. of wraps one value as a list. assoc(key, val, obj) copies an existing object and sets one key. objOf builds a brand-new one-key object and does not take a source object.
No. It always allocates a new object. Nested values are stored by reference, so mutating a nested object later still mutates the original nested value.
A computed property is data-first and not curried. R.objOf('status') is already a function of one value, ready for R.map, R.compose, and R.pipe.
No. R.objOf is a stable Ramda object helper from v0.18.0. It is not deprecated, experimental, or non-standard.