getPath

读取对象指定路径的值

用法

getPath 函数用于读取对象指定路径的值。

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 为 undefined,使用默认值)
getPath(obj, 'a.e', 100) // null(null 不触发默认值)
getPath(obj, 'arr[0].x') // 9
getPath(obj, '') // 返回 obj 本身

API

getPath(object, path, defaultValue?)

读取对象指定路径的值。

参数

object
T required
源对象。
path
PathInput required
路径字符串或片段数组。
defaultValue
D
结果为 undefined 时返回的默认值。

返回值

返回值
unknown | D
读取到的值或默认值。

Changelog

b6146 — refactor: 统一源码文件命名为 camelCase

Copyright © 2024 - 2026 YiXuan - MIT License