Skip to content

getByPath

Input an object obj and a field path path, then traverse the object deeply according to the path to retrieve the value. If the traversal is interrupted (e.g., the path is invalid) or the value is undefined or null, use defaultValue as the default value.

Added in v0.0.1

Usage

ts
import { getByPath } from 'parsnip-kit'

getByPath({ a: 1 }, 'a') // 1
getByPath([1], '[0]') // 1
getByPath({ b: [0, 1, 2] }, 'b[2]') // 2
getByPath({ a: [{ b: { c: 'test' } }] }, 'a[0].b.c') // 'test'
getByPath({ a: 1 }, 'a[0].b.c') // undefined
getByPath({ a: 1 }, 'a[0].b.c', 'test') // 'test'

API

Arguments

ArgTypeOptionalDefaultDescription
objobjectfalseundefinedObject to get value
pathstringfalseundefinedField path
defaultValueanytrueundefinedDefault value

Returns

Type
any