---
title: "getPath"
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/en/docs/helpers/path/get-path"
---
# getPath

> Read a value at a dot or bracket path inside a nested object, returning an optional default when the path does not resolve.

## Usage

`getPath` reads the value at a specified path inside an object.

```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 is undefined, default value is used)
getPath(obj, 'a.e', 100) // null (null does not trigger the default value)
getPath(obj, 'arr[0].x') // 9
getPath(obj, '') // returns obj itself
```

## API

### `getPath(object, path, defaultValue?)`

Read the value at a specified path inside an object.

### Parameters

**object** (`T`) *required*: The source object.

**path** (`PathInput`) *required*: A path string or array of segments.

**defaultValue** (`D`): The default value returned when the result is undefined.

### Returns

**returns** (`unknown | D`): The resolved value, or the default value if the path does not resolve.

## Changelog

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

---

- [GitHub](https://github.com/mhaibaraai/movk-core/blob/main/src/helpers/path/getPath.ts)


## Sitemap

See the full [sitemap](https://core.mhaibaraai.cn/sitemap.md) for all pages.
