Skip to main content

Pick Function

Data Manipulation2 functions

Utility Overview

  • Property Selection Made Simple
Pick functions create new objects by selecting only the properties you need, offering clean data transformation and immutable object manipulation!

🔍 Key Features:

  • Basic Pick: Select specific properties by key names
  • Pick By Predicate: Filter properties using custom logic
  • Type Safety: Full TypeScript support with compile-time checking
  • Immutability: Original objects remain unchanged
  • Multiple Implementations:
  • pick(obj, keys): Direct property selection by key array
  • pickBy(obj, predicate): Conditional selection using predicate function
  • Type-safe operations: Maintains property types and relationships
  • Real-World Applications:
  • API Responses: Extract only needed fields from large objects
  • Data Privacy: Remove sensitive fields before sending to client
  • Form Processing: Select user-input fields from larger state
  • Database Results: Pick specific columns from query results
  • Configuration Objects: Extract relevant settings for components
💡 Benefits:
  • Clean, readable data transformations
  • Immutable operations prevent side effects
  • Type safety prevents runtime errors
  • Memory efficient - only copies needed properties
  • Functional programming patterns
  • Easy to test and debug

Details

  • Property Selection Made Simple
Pick functions create new objects by selecting only the properties you need, offering clean data transformation and immutable object manipulation!

🔍 Key Features:

  • Basic Pick: Select specific properties by key names
  • Pick By Predicate: Filter properties using custom logic
  • Type Safety: Full TypeScript support with compile-time checking
  • Immutability: Original objects remain unchanged
  • Multiple Implementations:
  • pick(obj, keys): Direct property selection by key array
  • pickBy(obj, predicate): Conditional selection using predicate function
  • Type-safe operations: Maintains property types and relationships
  • Real-World Applications:
  • API Responses: Extract only needed fields from large objects
  • Data Privacy: Remove sensitive fields before sending to client
  • Form Processing: Select user-input fields from larger state
  • Database Results: Pick specific columns from query results
  • Configuration Objects: Extract relevant settings for components
💡 Benefits:
  • Clean, readable data transformations
  • Immutable operations prevent side effects
  • Type safety prevents runtime errors
  • Memory efficient - only copies needed properties
  • Functional programming patterns
  • Easy to test and debug

Category:Data Manipulation
Functions:pick, pickBy
Concepts:
object manipulationproperty selectionimmutabilitytype safety

Implementation

pick

Time: O(k) | Space: O(k)
export function pick<T extends Record<string, unknown>, K extends keyof T>(
  obj: T,
  keys: K[]
): Pick<T, K> {
  if (obj == null || typeof obj !== 'object') {
    throw new Error('First argument must be an object');
  }
  
  const result = {} as Pick<T, K>;
  
  for (const key of keys) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      (result as any)[key] = obj[key];
    }
  }
  
  return result;
}

Usage Examples

Basic property selection

const user = { id: 1, name: 'John', email: 'john@example.com', password: 'secret' };
const publicUser = pick(user, ['id', 'name', 'email']);
// { id: 1, name: 'John', email: 'john@example.com' }