getPath
Read a value at a dot or bracket path inside a nested object, returning an optional default when the path does not resolve.
Usage
getPath reads the value at a specified path inside an object.
import { getPath } from '@movk/core'
const obj = { a: { b: { c: 1, d: undefined }, e: null }, arr: [{ x: 9 }] }
getPath(obj, 'a.b.c') // 1
getPath(obj, 'a.b.d', 42) // 42 (d is undefined, default value is used)
getPath(obj, 'a.e', 100) // null (null does not trigger the default value)
getPath(obj, 'arr[0].x') // 9
getPath(obj, '') // returns obj itself
API
getPath(object, path, defaultValue?)
Read the value at a specified path inside an object.
Parameters
object
T required
The source object.
path
PathInput required
A path string or array of segments.
defaultValue
D
The default value returned when the result is
undefined.Returns
returns
unknown | D
The resolved value, or the default value if the path does not resolve.
Changelog
No recent changes