---
title: "toPath"
description: "将 a.b[0].c 格式的路径字符串解析为路径片段数组，用于对象路径访问操作。"
seo_title: "toPath"
seo_description: "Parse a path string into an array of segments, supporting dot and bracket notation, the basis for safe nested property access."
canonical_url: "https://core.mhaibaraai.cn/docs/helpers/path/to-path"
---
# toPath

> 将 a.b\[0\].c 格式的路径字符串解析为路径片段数组，用于对象路径访问操作。

## 用法

`toPath` 函数用于将路径字符串解析为片段数组。

```ts
import { toPath } from '@movk/core'

toPath('a.b[0].c') // ['a', 'b', 0, 'c']
toPath('a[\'x.y\']') // ['a', 'x.y']
```

## API

### `toPath(path)`

将路径字符串解析为片段数组。

### 参数

**path** (`PathInput`) *required*: 路径字符串或片段数组。

### 返回值

**返回值** (`PathSegments`): 解析后的片段数组。

## Changelog

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


## Sitemap

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