Removes undefined values from objects while preserving falsy values (0, false, null).
- ✅ Removes
undefined: Cleans your objects of undefined properties. - ✅ Preserves Falsy: Keeps
0,false,null, and"". - ✅ Type Safe: Returns a
Partial<T>of your input type. - ✅ Tiny & Fast: Zero dependencies.
- ✅ Dual Support: Works with both ES Modules (ESM) and CommonJS (CJS).
npm install pick-definedimport { pickDefined } from 'pick-defined';
const user = {
name: "Poly",
age: undefined,
active: false,
};
const cleaned = pickDefined(user);
// Result: { name: "Poly", active: false }Unlike some other libraries or JSON.stringify tricks, pick-defined keeps falsy values that are often valid data.
const filters = {
search: "", // Kept
page: 0, // Kept
limit: 10, // Kept
sort: undefined, // Removed
category: null // Kept
};
const query = pickDefined(filters);
// Result: { search: "", page: 0, limit: 10, category: null }const { pickDefined } = require('pick-defined');
const cleaned = pickDefined({ a: 1, b: undefined });- obj: The object to clean.
- Returns: A shallow copy of the object with all
undefinedproperties removed.
MIT
Author: Ahmer Arain