omit
Creates a new object by omitting specified keys from the source object. Returns a new object that contains all properties from the source object except for the specified keys. This is the opposite of the pick utility. The operation is shallow - nested objects are not deeply omitted.
Installation
npx fragmen add object/omitSource Code
/**
* Creates a new object by omitting specified keys from the source object.
*
* Returns a new object that contains all properties from the source object
* except for the specified keys. This is the opposite of the pick utility.
* The operation is shallow - nested objects are not deeply omitted.
*
* @tags pure, object-manipulation
* @param {T} obj The source object to omit properties from
* @param {K[]} keys Array of keys to omit from the object
* @returns {Omit<T, K>} A new object without the specified keys
*
* @example
* ```typescript
* const user = {
* id: 1,
* name: 'John',
* email: 'john@example.com',
* password: 'secret123',
* age: 30
* };
*
* const publicUser = omit(user, ['password', 'email']);
* console.log(publicUser); // { id: 1, name: 'John', age: 30 }
*
* const minimal = omit(user, ['password', 'email', 'age']);
* console.log(minimal); // { id: 1, name: 'John' }
*
* // Omitting non-existent keys is safe
* const safe = omit(user, ['nonexistent', 'password']);
* console.log(safe); // { id: 1, name: 'John', email: 'john@example.com', age: 30 }
* ```
*/
export function omit<T extends Record<string, unknown>, K extends keyof T>(
obj: T | null | undefined,
keys: K[]
): Omit<T, K> {
// Handle edge cases
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
return {} as Omit<T, K>;
}
if (!Array.isArray(keys) || keys.length === 0) {
return { ...obj } as Omit<T, K>;
}
const keysToOmit = new Set(keys);
const result = {} as Omit<T, K>;
for (const key in obj) {
if (
Object.prototype.hasOwnProperty.call(obj, key) &&
!keysToOmit.has(key as unknown as K)
) {
(result as Record<string, unknown>)[key] = obj[key];
}
}
return result;
}
Examples
const user = {
id: 1,
name: 'John',
email: 'john@example.com',
password: 'secret123',
age: 30
};
const publicUser = omit(user, ['password', 'email']);
console.log(publicUser); // { id: 1, name: 'John', age: 30 }
const minimal = omit(user, ['password', 'email', 'age']);
console.log(minimal); // { id: 1, name: 'John' }
// Omitting non-existent keys is safe
const safe = omit(user, ['nonexistent', 'password']);
console.log(safe); // { id: 1, name: 'John', email: 'john@example.com', age: 30 }
Related Utilities
clone
objectCreates a deep copy of an object. Recursively clones all properties of an object, including nested objects and arrays. Handles circular references by maintaining a reference map. Primitive values, functions, and built-in objects like Date and RegExp are handled appropriately.
pick
objectCreates a new object composed of the picked object properties. Extracts only the specified keys from the source object, creating a new object with just those properties. Non-existent keys are silently ignored.
has-path
objectChecks if a nested property path exists in an object. Safely traverses nested object properties using a dot-notation path string or an array of keys. Returns true if the path exists (even if the final value is undefined), false if any part of the path is missing.
deep-equal
objectPerforms deep equality comparison between two values
merge
objectChecks if a value is a non-array, non-null object.
chunk
arraySplits an array into chunks of a specified size. Creates a new array containing subarrays (chunks) of the original array, each with a maximum length of the specified size. The last chunk may contain fewer elements if the array length is not evenly divisible by the chunk size.
Quick Actions
Tags
Parameters
objTThe source object to omit properties from
keysK[]Array of keys to omit from the object
Returns
Omit<T, K>A new object without the specified keys