omit
從對象中排除指定的屬性,創建新對象。
語法
typescript
omit<T, K extends keyof T>(
object: T,
keys: K[]
): Omit<T, K>
參數
object
(T): 源對象keys
(K[]): 要排除的屬性鍵數組
返回值
Omit<T, K>
: 排除指定屬性後的新對象
示例
基本用法
typescript
import { omit } from 'radash'
const user = {
id: 1,
name: 'Alice',
email: 'alice@example.com',
password: 'secret123',
age: 25
}
const publicUser = omit(user, ['password', 'email'])
// { id: 1, name: 'Alice', age: 25 }
排除單個屬性
typescript
import { omit } from 'radash'
const product = {
id: 123,
name: 'Laptop',
price: 999,
category: 'Electronics'
}
const productWithoutId = omit(product, ['id'])
// { name: 'Laptop', price: 999, category: 'Electronics' }
排除多個屬性
typescript
import { omit } from 'radash'
const order = {
id: 'ORD-001',
customerId: 456,
items: ['item1', 'item2'],
total: 150.00,
status: 'pending',
createdAt: new Date()
}
const orderSummary = omit(order, ['customerId', 'createdAt'])
// { id: 'ORD-001', items: ['item1', 'item2'], total: 150.00, status: 'pending' }
處理嵌套對象
typescript
import { omit } from 'radash'
const user = {
id: 1,
profile: {
firstName: 'Alice',
lastName: 'Smith',
age: 25
},
settings: {
theme: 'dark',
language: 'en'
}
}
const userWithoutSettings = omit(user, ['settings'])
// { id: 1, profile: { firstName: 'Alice', lastName: 'Smith', age: 25 } }
處理不存在的屬性
typescript
import { omit } from 'radash'
const user = {
name: 'Alice',
age: 25
}
const result = omit(user, ['name', 'email', 'age'])
// {} (所有屬性都被排除)
處理空對象
typescript
import { omit } from 'radash'
const emptyObj = {}
const result = omit(emptyObj, ['name', 'age'])
// {}
處理空鍵數組
typescript
import { omit } from 'radash'
const user = {
name: 'Alice',
age: 25
}
const result = omit(user, [])
// { name: 'Alice', age: 25 } (沒有排除任何屬性)
處理函數屬性
typescript
import { omit } from 'radash'
const component = {
name: 'Button',
render: () => '<button>Click me</button>',
props: { text: 'Click me' }
}
const componentWithoutRender = omit(component, ['render'])
// { name: 'Button', props: { text: 'Click me' } }
處理數組屬性
typescript
import { omit } from 'radash'
const user = {
id: 1,
name: 'Alice',
hobbies: ['reading', 'swimming'],
tags: ['developer', 'frontend']
}
const userWithoutHobbies = omit(user, ['hobbies'])
// { id: 1, name: 'Alice', tags: ['developer', 'frontend'] }
處理日期屬性
typescript
import { omit } from 'radash'
const event = {
id: 1,
title: 'Meeting',
date: new Date('2023-01-01'),
duration: 60,
attendees: ['Alice', 'Bob']
}
const eventWithoutDate = omit(event, ['date'])
// { id: 1, title: 'Meeting', duration: 60, attendees: ['Alice', 'Bob'] }
處理布爾值屬性
typescript
import { omit } from 'radash'
const user = {
id: 1,
name: 'Alice',
isActive: true,
isAdmin: false,
email: 'alice@example.com'
}
const userWithoutStatus = omit(user, ['isActive', 'isAdmin'])
// { id: 1, name: 'Alice', email: 'alice@example.com' }
處理null和undefined值
typescript
import { omit } from 'radash'
const user = {
name: 'Alice',
email: null,
phone: undefined,
age: 25
}
const userWithoutNulls = omit(user, ['email', 'phone'])
// { name: 'Alice', age: 25 }
處理Symbol屬性
typescript
import { omit } from 'radash'
const sym = Symbol('key')
const obj = {
[sym]: 'value',
name: 'Alice',
age: 25
}
const result = omit(obj, [sym, 'name'])
// { age: 25 }
處理復雜對象
typescript
import { omit } from 'radash'
const complexObject = {
id: 1,
metadata: {
createdAt: new Date(),
version: '1.0.0'
},
data: {
items: [1, 2, 3],
total: 100
},
config: {
theme: 'dark',
language: 'en'
}
}
const essentialData = omit(complexObject, ['metadata', 'config'])
// { id: 1, data: { items: [1, 2, 3], total: 100 } }
處理API響應
typescript
import { omit } from 'radash'
const apiResponse = {
data: {
users: [
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' }
],
total: 2,
page: 1,
limit: 10
},
status: 200,
message: 'Success',
timestamp: new Date()
}
const userData = omit(apiResponse, ['status', 'message', 'timestamp'])
// { data: { users: [...], total: 2, page: 1, limit: 10 } }
處理表單數據
typescript
import { omit } from 'radash'
const formData = {
firstName: 'Alice',
lastName: 'Smith',
email: 'alice@example.com',
password: 'secret123',
confirmPassword: 'secret123',
terms: true
}
const publicInfo = omit(formData, ['password', 'confirmPassword', 'terms'])
// { firstName: 'Alice', lastName: 'Smith', email: 'alice@example.com' }
數據清理
typescript
import { omit } from 'radash'
function cleanSensitiveData(data: any) {
const sensitiveFields = ['password', 'token', 'secret', 'privateKey']
return omit(data, sensitiveFields)
}
const userData = {
id: 1,
name: 'Alice',
email: 'alice@example.com',
password: 'secret123',
token: 'abc123',
preferences: { theme: 'dark' }
}
const cleanData = cleanSensitiveData(userData)
// { id: 1, name: 'Alice', email: 'alice@example.com', preferences: { theme: 'dark' } }
條件排除
typescript
import { omit } from 'radash'
function createUserResponse(user: any, includePrivate = false) {
const privateFields = ['password', 'email', 'phone']
if (includePrivate) {
return user
}
return omit(user, privateFields)
}
const user = {
id: 1,
name: 'Alice',
email: 'alice@example.com',
password: 'secret123',
phone: '123-456-7890'
}
const publicResponse = createUserResponse(user, false)
// { id: 1, name: 'Alice' }
const privateResponse = createUserResponse(user, true)
// { id: 1, name: 'Alice', email: 'alice@example.com', password: 'secret123', phone: '123-456-7890' }
注意事項
- 不可變性: 不會修改原對象
- 類型安全: 支持完整的 TypeScript 類型推斷
- 不存在的屬性: 會忽略不存在的屬性
- 淺拷貝: 只進行淺拷貝,嵌套對象會共享引用
與其他函數的區別
omit
: 排除指定的屬性pick
: 選擇指定的屬性keys
: 獲取對象的所有鍵values
: 獲取對象的所有值
性能
- 時間復雜度: O(n),其中 n 是鍵的數量
- 空間復雜度: O(n)
- 適用場景: 數據過濾、隱私保護、API響應處理