Function omit

  • Creates a new object by omitting specified keys from the source object.

    This function returns a new object that includes all properties from the source object except for the keys specified in the keysToOmit array.

    Type Parameters

    • T

      The type of the source object.

    Parameters

    • obj: T

      The source object from which keys should be omitted.

    • keysToOmit: (keyof T)[]

      An array of keys to omit from the source object.

    Returns Partial<T>

    A new object with the specified keys omitted.

    // Example 1: Omitting specific keys
    const user = { id: 1, name: 'John', age: 30, email: 'john@example.com' };
    const result = omit(user, ['age', 'email']);
    console.log(result); // { id: 1, name: 'John' }

    // Example 2: Omitting no keys
    const data = { key: 'value', flag: true };
    const noOmission = omit(data, []);
    console.log(noOmission); // { key: 'value', flag: true }

    // Example 3: Edge case with non-existent keys
    const sample = { a: 1, b: 2 };
    const omitted = omit(sample, ['c']);
    console.log(omitted); // { a: 1, b: 2 }