omit
Exclude specified properties from an object, creating a new object.
Syntax
typescript
omit<T, K extends keyof T>(
object: T,
keys: K[]
): Omit<T, K>
Parameters
object
(T): Source objectkeys
(K[]): Array of property keys to exclude
Return Value
Omit<T, K>
: New object after excluding specified properties
Examples
Basic Usage
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 }
Exclude Single Property
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' }
Exclude Multiple Properties
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' }
Handle Nested Objects
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 } }
Handle Non-existent Properties
typescript
import { omit } from 'radash'
const user = {
name: 'Alice',
age: 25
}
const result = omit(user, ['name', 'email', 'age'])
// {} (all properties excluded)
Handle Empty Object
typescript
import { omit } from 'radash'
const emptyObj = {}
const result = omit(emptyObj, ['name', 'age'])
// {}
Handle Empty Key Array
typescript
import { omit } from 'radash'
const user = {
name: 'Alice',
age: 25
}
const result = omit(user, [])
// { name: 'Alice', age: 25 } (no properties excluded)
Handle Function Properties
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' } }
Handle Array Properties
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'] }
Handle Date Properties
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'] }
Handle Boolean Properties
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' }
Handle null and undefined values
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 }
Handle Symbol Properties
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 }
Handle Complex Objects
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 } }
Handle API Responses
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 } }
Handle Form Data
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' }
Data Cleaning
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' } }
Conditional Exclusion
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' }
Notes
- Immutability: Does not modify the original object
- Type Safety: Supports full TypeScript type inference
- Non-existent Properties: Ignores non-existent properties
- Shallow Copy: Performs a shallow copy, nested objects share references
Differences from Other Functions
omit
: Excludes specified propertiespick
: Selects specified propertieskeys
: Gets all keys of an objectvalues
: Gets all values of an object
Performance
- Time Complexity: O(n), where n is the number of keys
- Space Complexity: O(n)
- Applicable Scenarios: Data filtering, privacy protection, API response handling