pick
Creates a new object composed of the picked object properties. Extracts only the specified keys from the source object, creating a new object with just those properties. Non-existent keys are silently ignored.
Installation
npx fragmen add object/pickSource Code
/**
* Creates a new object composed of the picked object properties.
*
* Extracts only the specified keys from the source object, creating a new object
* with just those properties. Non-existent keys are silently ignored.
*
* @tags pure, object-manipulation
* @param {T} obj The source object to pick properties from.
* @param {K[]} keys Array of property names to pick from the source object.
* @returns {Pick<T, K>} A new object containing only the specified properties.
*
* @example
* ```typescript
* const user = { id: 1, name: 'John', email: 'john@example.com', age: 30 };
* const publicInfo = pick(user, ['id', 'name']);
* // Result: { id: 1, name: 'John' }
*
* const nonExistent = pick(user, ['id', 'role']); // 'role' doesn't exist
* // Result: { id: 1 }
* ```
*/
export function pick<T extends object, K extends keyof T>(
obj: T,
keys: K[]
): Pick<T, K> {
const result = {} as Pick<T, K>;
for (const key of keys) {
if (key in obj) {
result[key] = obj[key];
}
}
return result;
}
Examples
const user = { id: 1, name: 'John', email: 'john@example.com', age: 30 };
const publicInfo = pick(user, ['id', 'name']);
// Result: { id: 1, name: 'John' }
const nonExistent = pick(user, ['id', 'role']); // 'role' doesn't exist
// Result: { id: 1 }
Related Utilities
clone
objectCreates a deep copy of an object. Recursively clones all properties of an object, including nested objects and arrays. Handles circular references by maintaining a reference map. Primitive values, functions, and built-in objects like Date and RegExp are handled appropriately.
omit
objectCreates a new object by omitting specified keys from the source object. Returns a new object that contains all properties from the source object except for the specified keys. This is the opposite of the pick utility. The operation is shallow - nested objects are not deeply omitted.
has-path
objectChecks if a nested property path exists in an object. Safely traverses nested object properties using a dot-notation path string or an array of keys. Returns true if the path exists (even if the final value is undefined), false if any part of the path is missing.
deep-equal
objectPerforms deep equality comparison between two values
merge
objectChecks if a value is a non-array, non-null object.
chunk
arraySplits an array into chunks of a specified size. Creates a new array containing subarrays (chunks) of the original array, each with a maximum length of the specified size. The last chunk may contain fewer elements if the array length is not evenly divisible by the chunk size.
Quick Actions
Tags
Parameters
objTThe source object to pick properties from.
keysK[]Array of property names to pick from the source object.
Returns
Pick<T, K>A new object containing only the specified properties.