---
title: "getPath"
description: "读取对象指定路径的嵌套值，支持点分隔和数组索引路径，路径不存在时返回 undefined。"
seo_title: "getPath"
seo_description: "Read a value at a dot or bracket path inside a nested object, returning an optional default when the path does not resolve."
canonical_url: "https://core.mhaibaraai.cn/docs/helpers/path/get-path"
---
# getPath

> 读取对象指定路径的嵌套值，支持点分隔和数组索引路径，路径不存在时返回 undefined。

## 用法

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

```ts
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

See commit history for [src/helpers/path/getPath.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/helpers/path/getPath.ts).


## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
