pick
Select specified properties from an object, creating a new object.
Syntax
typescript
pick<T, K extends keyof T>(
object: T,
keys: K[]
): Pick<T, K>
Parameters
object
(T): Source objectkeys
(K[]): Array of property keys to select
Return Value
Pick<T, K>
: New object containing specified properties
Examples
Basic Usage
typescript
import { pick } from 'radash'
const user = {
id: 1,
name: 'Alice',
email: 'alice@example.com',
age: 25,
city: 'New York'
}
const userInfo = pick(user, ['name', 'email'])
// { name: 'Alice', email: 'alice@example.com' }
Select Single Property
typescript
import { pick } from 'radash'
const product = {
id: 123,
name: 'Laptop',
price: 999,
category: 'Electronics'
}
const productName = pick(product, ['name'])
// { name: 'Laptop' }
Select Multiple Properties
typescript
import { pick } from 'radash'
const order = {
id: 'ORD-001',
customerId: 456,
items: ['item1', 'item2'],
total: 150.00,
status: 'pending',
createdAt: new Date()
}
const orderSummary = pick(order, ['id', 'total', 'status'])
// { id: 'ORD-001', total: 150.00, status: 'pending' }
Handle Nested Objects
typescript
import { pick } from 'radash'
const user = {
id: 1,
profile: {
firstName: 'Alice',
lastName: 'Smith',
age: 25
},
settings: {
theme: 'dark',
language: 'en'
}
}
const userProfile = pick(user, ['profile'])
// { profile: { firstName: 'Alice', lastName: 'Smith', age: 25 } }
Handle Non-existent Properties
typescript
import { pick } from 'radash'
const user = {
name: 'Alice',
age: 25
}
const result = pick(user, ['name', 'email', 'age'])
// { name: 'Alice', age: 25 }
// Note: Non-existent property 'email' will be ignored
Handle Empty Object
typescript
import { pick } from 'radash'
const emptyObj = {}
const result = pick(emptyObj, ['name', 'age'])
// {}
Handle Empty Key Array
typescript
import { pick } from 'radash'
const user = {
name: 'Alice',
age: 25
}
const result = pick(user, [])
// {}
Handle Function Properties
typescript
import { pick } from 'radash'
const component = {
name: 'Button',
render: () => '<button>Click me</button>',
props: { text: 'Click me' }
}
const componentInfo = pick(component, ['name', 'props'])
// { name: 'Button', props: { text: 'Click me' } }
Handle Array Properties
typescript
import { pick } from 'radash'
const user = {
id: 1,
name: 'Alice',
hobbies: ['reading', 'swimming'],
tags: ['developer', 'frontend']
}
const userBasic = pick(user, ['name', 'hobbies'])
// { name: 'Alice', hobbies: ['reading', 'swimming'] }
Handle Date Properties
typescript
import { pick } from 'radash'
const event = {
id: 1,
title: 'Meeting',
date: new Date('2023-01-01'),
duration: 60,
attendees: ['Alice', 'Bob']
}
const eventInfo = pick(event, ['title', 'date'])
// { title: 'Meeting', date: new Date('2023-01-01') }
Handle Boolean Properties
typescript
import { pick } from 'radash'
const user = {
id: 1,
name: 'Alice',
isActive: true,
isAdmin: false,
email: 'alice@example.com'
}
const userStatus = pick(user, ['isActive', 'isAdmin'])
// { isActive: true, isAdmin: false }
Handle null and undefined values
typescript
import { pick } from 'radash'
const user = {
name: 'Alice',
email: null,
phone: undefined,
age: 25
}
const userContact = pick(user, ['email', 'phone', 'age'])
// { email: null, phone: undefined, age: 25 }
Handle Symbol Properties
typescript
import { pick } from 'radash'
const sym = Symbol('key')
const obj = {
[sym]: 'value',
name: 'Alice',
age: 25
}
const result = pick(obj, [sym, 'name'])
// { [Symbol(key)]: 'value', name: 'Alice' }
Handle Complex Objects
typescript
import { pick } 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 = pick(complexObject, ['id', 'metadata', 'data'])
// {
// id: 1,
// metadata: { createdAt: new Date(), version: '1.0.0' },
// data: { items: [1, 2, 3], total: 100 }
// }
Handle API Responses
typescript
import { pick } 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 = pick(apiResponse, ['data', 'status'])
// {
// data: { users: [...], total: 2, page: 1, limit: 10 },
// status: 200
// }
Handle Form Data
typescript
import { pick } from 'radash'
const formData = {
firstName: 'Alice',
lastName: 'Smith',
email: 'alice@example.com',
password: 'secret123',
confirmPassword: 'secret123',
terms: true
}
const publicInfo = pick(formData, ['firstName', 'lastName', 'email'])
// { firstName: 'Alice', lastName: 'Smith', email: 'alice@example.com' }
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
pick
: Selects specified propertiesomit
: Excludes 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, API response handling