---
title: "separate"
description: "将对象按指定键分离为两个对象"
seo_title: "separate"
seo_description: "Split an object into picked and omitted parts by the given keys, returning both in one call, ideal for separating sensitive fields."
canonical_url: "https://core.mhaibaraai.cn/docs/helpers/object/separate"
---
# separate

> 将对象按指定键分离为两个对象

## 用法

`separate` 函数用于将对象按指定键分离为两个对象。

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

const user = {
  id: 1,
  name: 'John',
  email: 'john@example.com',
  password: 'secret',
  role: 'admin'
}

const { picked, omitted } = separate(user, ['id', 'name'])
console.log(picked) // { id: 1, name: 'John' }
console.log(omitted) // { email: 'john@example.com', password: 'secret', role: 'admin' }

// 用于分离敏感信息
const { picked: publicData, omitted: privateData } = separate(user, ['id', 'name', 'email'])
```

## API

### `separate(obj, keys)`

将对象按指定键分离为两个对象。

### 参数

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

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

### 返回值

**返回值** (`{ picked: PickByKey<T, K>, omitted: OmitByKey<T, K> }`): 包含 picked 和 omitted 两个对象的结果。

## Changelog

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


## Sitemap

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