---
title: "pick"
description: "从对象中选择指定的键，返回新对象"
seo_title: "pick"
seo_description: "Return a new object containing only the given keys from the source, a type-safe way to project a subset of an object’s fields."
canonical_url: "https://core.mhaibaraai.cn/docs/helpers/object/pick"
---
# pick

> 从对象中选择指定的键，返回新对象

## 用法

`pick` 函数用于从对象中选择指定的键，返回新对象。

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

const user = {
  id: 1,
  name: 'John',
  email: 'john@example.com',
  password: 'secret',
  createdAt: '2023-01-01',
  updatedAt: '2023-01-15'
}

const publicInfo = pick(user, ['id', 'name', 'email'])
console.log(publicInfo) // { id: 1, name: 'John', email: 'john@example.com' }

const basicInfo = pick(user, ['id', 'name'])
console.log(basicInfo) // { id: 1, name: 'John' }
```

## API

### `pick<T, K>(obj, keys)`

从对象中选择指定的键，返回新对象。

### 参数

**obj** (`T extends AnyObject`) *required*: 源对象。

**keys** (`K[]`) *required*: 要选择的键数组。

### 返回值

**返回值** (`PickByKey<T, K>`): 只包含指定键的新对象。

## Changelog

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


## Sitemap

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