getPath
Category
Export size
min 0.83 kB · gzip 0.53 kB
See also
Reads the value at path, returning defaultValue when the path does not resolve or resolves to undefined. Descending stops at null/undefined instead of throwing.
A path is a dot/bracket string ('user.id', 'list[0].x') or an array of segments (['user', 'id']).
ts
import { getPath } from 'feathers-utils/helpers';Example
ts
import { getPath } from 'feathers-utils/helpers'
getPath({ user: { id: 1 } }, 'user.id') // => 1
getPath({ list: [{ x: 1 }] }, 'list[0].x') // => 1
getPath({ user: null }, 'user.id', 0) // => 0Type declaration
Show Type Declarations
ts
/**
* Reads the value at `path`, returning `defaultValue` when the path does not
* resolve or resolves to `undefined`. Descending stops at `null`/`undefined`
* instead of throwing.
*
* A path is a dot/bracket string (`'user.id'`, `'list[0].x'`) or an array of
* segments (`['user', 'id']`).
*
* @example
* ```ts
*
*
* getPath({ user: { id: 1 } }, 'user.id') // => 1
* getPath({ list: [{ x: 1 }] }, 'list[0].x') // => 1
* getPath({ user: null }, 'user.id', 0) // => 0
* ```
*
* @see https://utils.feathersjs.com/helpers/get-path.html
*/
export declare const getPath: <T = any>(
object: unknown,
path: PropertyPath,
defaultValue?: T,
) => T| Argument | Type | Description |
|---|---|---|
| object | unknown | |
| path | PropertyPath | |
| defaultValue | T |
Coming from lodash
A drop-in for _.get: same path syntax, same defaultValue semantics — the default is used for a missing path and for a value that is undefined, but not for null or other falsy values. A key that literally contains a dot ({ 'a.b': 1 }) wins over the nested reading, as in lodash.
