Back to Snippets
CODE

Object Property Omission (Omit)

Description

Creates a new object excluding specified properties, useful for removing sensitive or unnecessary fields

Code

/**
 * Omit specified properties from object
 * @param obj Target object
 * @param keys Array of properties to remove
 * @returns New object without specified properties
 */
function omitObject<T extends object, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {
  const result = { ...obj };
  keys.forEach(key => {
    delete result[key];
  });
  return result;
}

// Usage Example
const user = { id: 1, name: "John", password: "secret", token: "xyz123" };
const publicUser = omitObject(user, ["password", "token"]);
console.log(publicUser); // { id: 1, name: "John" }

Usage

Ideal for removing sensitive data (passwords, tokens) from objects before display or transmission

Tags

Object ManipulationProperty OmissionData Security