25 lines
659 B
TypeScript
Executable File
25 lines
659 B
TypeScript
Executable File
/**
|
|
* Pick specified keys from an object.
|
|
*
|
|
* @param names - An array of keys to pick from the object.
|
|
* @param obj - The object from which to pick the keys.
|
|
* @returns The new object with the specified keys picked.
|
|
*
|
|
* @example
|
|
* const input = { a: 1, b: 2, c: 3 };
|
|
* const pickedKeys = ['a', 'c'];
|
|
* const output = pick(pickedKeys, input);
|
|
* // output: { a: 1, c: 3 }
|
|
*/
|
|
export function pick(names: string[], obj: Record<string, unknown>): object {
|
|
const result: Record<string, unknown> = {};
|
|
let idx = 0;
|
|
|
|
while (idx < names.length) {
|
|
if (names[idx] in obj) result[names[idx]] = obj[names[idx]];
|
|
idx += 1;
|
|
}
|
|
|
|
return result;
|
|
}
|