Add initial project files
This commit is contained in:
Executable
+24
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
Reference in New Issue
Block a user