Pick and Omit Object Properties
Pick and Omit Object Properties is a reusable JavaScript snippet. Creates a new object by picking or omitting specified keys. Useful for filtering data and creating subsets. The full implementation: function pick(obj, keys) { return keys.reduce((acc, key) => { if (key in obj) acc[key] = obj[key] return acc }, {}) } function omit(obj, keys) { const keySet = new Set(keys) return Object.keys(obj) .filter(k => !keySet.has(k)) .reduce((acc, k) => { acc[k] = obj[k]; return acc }, {}) } // Usage const user = { id: 1, name: 'Alice', password: 'secret', role: 'admin' } pick(user, ['id', 'name']) // { id: 1, name: 'Alice' } omit(user, ['password']) // { id: 1, name: 'Alice',… This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.