Skip to content

Replacements for dot-prop

dlv and dset

dlv gets nested values with default fallbacks and dset sets nested values with automatic intermediate object creation.

ts
import { getProperty, setProperty } from 'dot-prop'
import delve from 'dlv'
import { dset } from 'dset'

const value = getProperty(obj, 'foo.bar.baz') 
const value = delve(obj, 'foo.bar.baz') 

setProperty(obj, 'foo.bar.baz', 'value') 
dset(obj, 'foo.bar.baz', 'value') 

String.prototype.split + Array.prototype.reduce (native)

If you only need to get a nested value you can use a simple function:

js
function getProperty(obj, key) {
  return key.split('.').reduce((acc, k) => acc?.[k], obj)
}

const value = getProperty(obj, 'foo.bar.baz')

NOTE

This assumes that you do not consume dot paths as user input. If you do, ensure you sanitize keys before accessing them (e.g. through Object.hasOwn).

Released under the MIT License. (1f304c6e)