Object Property Omission Utility

2.0.2 · maintenance · verified Sun Apr 19

omit.js is a lightweight JavaScript utility function designed to create a shallow copy of an object, excluding specified fields. The current stable version is 2.0.2. This package provides a straightforward API for basic object manipulation, returning a new object without mutating the original. Its release cadence has been very slow, with the last update in 2018. Key differentiators include its minimal footprint and singular focus on shallow property omission. It is not intended for deep cloning or complex data transformations, focusing purely on filtering top-level properties. Its utility can be partially superseded by native JavaScript spread syntax for simple cases, and TypeScript's built-in `Omit` utility type for type manipulation.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use `omit.js` to filter properties from an object, creating a shallow copy. It shows omitting multiple and single fields, and highlights the non-mutating behavior on the top-level object, while also noting the shallow copy aspect for nested objects.

import omit from 'omit.js';

const userData = {
  id: 'user-123',
  username: 'jsmith',
  email: 'jsmith@example.com',
  passwordHash: 'somehashedvalue',
  createdAt: new Date(),
  settings: { theme: 'dark', notifications: true }
};

// Use omit.js to create a new object without sensitive or unnecessary fields
const publicProfile = omit(userData, ['passwordHash', 'email', 'createdAt']);

console.log('Public Profile:', publicProfile);
// Expected: { id: 'user-123', username: 'jsmith', settings: { theme: 'dark', notifications: true } }

// The original object remains untouched
console.log('Original User Data:', userData);

const postData = {
  title: 'My First Post',
  content: 'Hello World!',
  authorId: 'user-123',
  tags: ['javascript', 'webdev']
};

// Omit a single field
const postWithoutAuthor = omit(postData, ['authorId']);
console.log('Post without Author:', postWithoutAuthor);

// Important: omit.js creates a shallow copy. Nested objects like 'settings'
// in 'publicProfile' are copied by reference. Changes to 'userData.settings'
// after calling omit will be reflected in 'publicProfile.settings'.

view raw JSON →