# @cookbook/dot-notation 中文文档

规范地址：https://npmcn.com/package/%40cookbook/dot-notation/

版本：2\.0\.1
npm 元数据更新时间：2022-06-19T17:34:46.222Z
中文译文更新时间：2026-11-10T19:50:11.674Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40cookbook/dot-notation
原文对照：https://npmcn.com/package/%40cookbook/dot-notation/?lang=original
项目主页：https://github.com/the-cookbook/dot-notation#readme
源码仓库：https://github.com/the-cookbook/dot-notation

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：58.3 kB，50 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=8
- 运行依赖：0 个
- 首次发布：2020-07-10T18:48:06.918Z
- 最新版本发布：2022-06-19T17:34:46.093Z
- 近 30 天下载量：3051（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：5（近 12 个月 0 个）

## 中文简介

对象读取和复杂的转换使用点符号语法.

## 安装

```sh
npm install @cookbook/dot-notation
```

也可以使用：`pnpm add @cookbook/dot-notation`、`yarn add @cookbook/dot-notation`、`bun add @cookbook/dot-notation`

## README

# @ cookbook/ dot - 注释

> 通过使用使对象读取和复杂转换变得容易 `dot.notation.syntax[]`

[![NPM Version](https://img.shields.io/npm/v/@cookbook/dot-notation.svg?style=flat-square)](https://npmjs.org/package/@cookbook/dot-notation) [![CI Status](https://circleci.com/gh/the-cookbook/dot-notation.svg?style=svg)](https://circleci.com/gh/the-cookbook/dot-notation) [![Downloads Stats](https://img.shields.io/npm/dm/@cookbook/dot-notation.svg?style=flat-square)](https://npmjs.org/package/@cookbook/dot-notation) [![GitHub stars](https://img.shields.io/github/stars/the-cookbook/dot-notation.svg)](https://github.com/the-cookbook/dot-notation/stargazers) [![Known Vulnerabilities](https://snyk.io/test/github/the-cookbook/dot-notation/badge.svg)](https://snyk.io/test/github/the-cookbook/dot-notation) [![GitHub issues](https://img.shields.io/github/issues/the-cookbook/dot-notation.svg)](https://github.com/the-cookbook/dot-notation/issues) [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/the-cookbook/dot-notation) [![install size](https://packagephobia.now.sh/badge?p=@cookbook/dot-notation)](https://packagephobia.now.sh/result?p=@cookbook/dot-notation) [![gzip size](http://img.badgesize.io/https://unpkg.com/@cookbook/dot-notation/lib/dot.min.js?compression=gzip)](https://unpkg.com/@cookbook/dot-notation/lib/dot.min.js)

![](https://npmcn.com/package/%40cookbook/dot-notation/dot-notation.png)

## 演示

与游戏 _点注_ 经验 **魔术**来啊!

[![Edit @cookbook/dot-notation](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/cookbookdot-notation-kjwd2?fontsize=14&hidenavigation=1&theme=dark)

## 安装

```sh
npm install @cookbook/dot-notation --save
#or
yarn add @cookbook/dot-notation
```

## 如何使用

### 选择一个值

```js
import { pick } from '@cookbook/dot-notation';

const source = {
  person: {
    name: {
      firstName: 'John',
      lastName: 'Doe'
    },
    address: [
      {
        street: 'Infinite Loop',
        city: 'Cupertino',
        state: 'CA',
        postalCode: 95014,
        country: 'United States'
      },
    ]
  }
};

pick(source, 'person.name');
//output { firstName: 'John', lastName: 'Doe' }

pick(source, 'person.address[0].street');
//output "Infinite Loop"
```

### 分析对象

#### 常规分析

```js
import { parse } from '@cookbook/dot-notation';

const source = {
  'person.name.firstName': 'John',
  'person.name.lastName': 'Doe',
  'person.address[].street': 'Infinite Loop',
  'person.address[].city': 'Cupertino',
  'person.address[].postalCode': 95014,
};

parse(source);

/* output
{
  person: {
    name: {
      firstName: 'John',
      lastName: 'Doe',
    },
    address: [
      {
        street: 'Infinite Loop',
        city: 'Cupertino',
        postalCode: 95014,
      },
    ],
  },
}
*/
```

#### 以嵌入式阵列

> 地点 `[n]` 代表用于插入元素的数组索引位置

```js
import { parse } from '@cookbook/dot-notation';

const source = {
  '[0].street': 'Infinite Loop',
  '[0].city': 'Cupertino',
  '[0].postalCode': 95014,
  '[1].street': '1600 Amphitheatre',
  '[1].city': 'Mountain View',
  '[1].postalCode': 94043,
  '[2][0]': 'hobbies',
  '[2][1][0]': ['coding'],
  '[2][1][1]': ['gaming'],
};

parse(source);

/* output
[
 {
  "postalCode": 95014,
  "city": "Cupertino",
  "street": "Infinite Loop"
 },
 {
  "postalCode": 94043,
  "city": "Mountain View",
  "street": "1600 Amphitheatre"
 },
 [
  "hobbies",
  [["coding"],["gaming"]]
 ]
]
*/
```

### 解析单键

```js
import { parseKey } from '@cookbook/dot-notation';

const path = 'person.name';
const value = 'John Doe';

parseKey(path, value);

/* output
{
  person: {
    name: 'John Doe',
  },
}
*/
```

## 常见问题

### @cookbook/dot-notation 是什么？

对象读取和复杂的转换使用点符号语法\.

### 如何安装 @cookbook/dot-notation？

在项目目录运行 npm install @cookbook/dot-notation，或使用 pnpm add / yarn add / bun add。

### @cookbook/dot-notation 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @cookbook/dot-notation 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @cookbook/dot-notation 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=8。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @cookbook/dot-notation 可以商用吗？

@cookbook/dot-notation 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @cookbook/dot-notation 的下载量有多少？

根据 npm 官方下载统计，@cookbook/dot-notation 在 2026/10/11 – 2026/11/09 这 30 天内被下载 3,051 次，日均约 102 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @cookbook/dot-notation 还在维护吗？

最新版本 2\.0\.1 发布于 2022/06/20（4 年前）；至今共发布 5 个版本，其中过去 12 个月发布了 0 个；首次发布于 2020/07/11。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

