JavaScript

Pick & Omit Object Keys

by @admin
12h ago
Apr 28, 2026
Public
pick returns a new object containing only the specified keys. omit returns a new object with the specified keys removed. Both are pure (non-mutating) and handle missing keys gracefully. Essential for cleaning API payloads, projecting data shapes, and avoiding over-sending sensitive fields.
JavaScript
const pick = (obj, keys) =>
  Object.fromEntries(
    keys.filter((k) => k in obj).map((k) => [k, obj[k]])
  );

const omit = (obj, keys) => {
  const set = new Set(keys);
  return Object.fromEntries(
    Object.entries(obj).filter(([k]) => !set.has(k))
  );
};

// Usage
const user = { id: 1, name: 'Alice', password: 'secret', role: 'admin' };
console.log(pick(user, ['id', 'name']));          // { id: 1, name: 'Alice' }
console.log(omit(user, ['password']));            // { id: 1, name: 'Alice', role: 'admin' }
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.