common-utility-functions
    Preparing search index...

    Function pickProperties

    • Creates a new object containing only the specified properties from the source object. This is useful for creating subsets of objects, filtering out unwanted properties, or preparing objects for serialization. Properties that don't exist on the source object are safely ignored.

      Type Parameters

      • K extends string
      • T extends Record<string | K, unknown>

      Parameters

      • obj: T

        The source object to pick properties from

      • properties: K[]

        An array of property names to include in the new object

      Returns Partial<T>

      A new object containing only the specified properties that exist on the source object

      const user = {
      id: 1,
      name: 'John Doe',
      email: 'john@example.com',
      password: 'secret123',
      lastLogin: '2023-01-01',
      isActive: true
      };

      // Pick only safe properties for API response
      const publicUser = pickProperties(user, ['id', 'name', 'email', 'isActive']);
      // Result: { id: 1, name: 'John Doe', email: 'john@example.com', isActive: true }

      // Pick non-existent properties (safely ignored)
      const partial = pickProperties(user, ['name', 'nonExistent']);
      // Result: { name: 'John Doe' }

      // Empty properties array returns empty object
      const empty = pickProperties(user, []);
      // Result: {}

      // Useful for form data extraction
      const formData = { name: 'Jane', email: 'jane@test.com', csrf: 'token123' };
      const cleanData = pickProperties(formData, ['name', 'email']);
      // Result: { name: 'Jane', email: 'jane@test.com' }

      1.0.0