codetrix
Version:
A lightweight lodash-style utility library
20 lines (19 loc) • 471 B
JavaScript
/**
* Creates a new object composed of selected keys from the original object.
*
* @param obj - The source object.
* @param keys - The keys to pick.
* @returns A new object with only the picked keys.
*
* @example
* pick({ a: 1, b: 2, c: 3 }, ['a', 'c']); // { a: 1, c: 3 }
*/
export function pick(obj, keys) {
const result = {};
for (const key of keys) {
if (key in obj) {
result[key] = obj[key];
}
}
return result;
}