Skip to content

getProperty

Safely get a nested property value.

Usage

js
import { getProperty } from 'js-cool'

Signature

typescript
function getProperty(obj: any, path: string, defaultValue?: any): any

Parameters

ParameterTypeDescription
objanySource object
pathstringProperty path (e.g. 'a.b.c')
defaultValueanyDefault if not found

Returns

any - The property value or default.

Examples

js
const obj = { a: { b: { c: 1 } } }

getProperty(obj, 'a.b.c') // 1
getProperty(obj, 'a.b.d', 'default') // 'default'
getProperty(obj, 'x.y.z') // undefined

Released under the MIT License.