Extract Helper Function in TypeScript
This challenge asks you to create a reusable TypeScript helper function called extract. The extract function will take an object and a list of keys as input, and return a new object containing only the specified keys and their corresponding values from the original object. This is a common utility for data transformation and filtering, particularly when working with APIs or complex data structures.
Problem Description
You need to implement the extract function in TypeScript. The function should accept two arguments:
obj: An object of typeRecord<string, any>. This represents the input object from which you'll extract data.keys: An array of strings. Each string in this array represents a key that you want to extract from the input object.
The function should return a new object of type Record<string, any>. This new object should contain only the key-value pairs where the key exists in both the input object obj and the keys array. The order of keys in the returned object should match the order of keys in the keys array.
Key Requirements:
- The function must be type-safe, leveraging TypeScript's type system to ensure correct input and output types.
- The function should handle cases where a key in the
keysarray does not exist in the input objectobj. In such cases, the key should be omitted from the returned object. - The function should not modify the original input object
obj. It should return a new object. - The function should be efficient, avoiding unnecessary iterations or operations.
Expected Behavior:
The function should iterate through the keys array and, for each key, check if it exists in the obj. If the key exists, the corresponding key-value pair should be added to the new object. If the key does not exist, it should be skipped.
Edge Cases to Consider:
- Empty
keysarray: Should return an empty object. objis null or undefined: Should return an empty object. (Handle gracefully - don't throw errors).keysarray contains duplicate keys: The first occurrence of the key in thekeysarray should be used. Subsequent occurrences should be ignored.- Keys with different casing (e.g., "name" vs. "Name"): The comparison should be case-sensitive.
Examples
Example 1:
Input: obj = { name: "John", age: 30, city: "New York" }, keys = ["name", "city"]
Output: { name: "John", city: "New York" }
Explanation: The function extracts the "name" and "city" properties from the input object and returns a new object containing only those properties.
Example 2:
Input: obj = { name: "Alice", age: 25 }, keys = ["name", "address", "age"]
Output: { name: "Alice", age: 25 }
Explanation: The function extracts the "name" and "age" properties because they exist in the input object. The "address" property is omitted because it does not exist.
Example 3:
Input: obj = { name: "Bob", age: 40 }, keys = []
Output: {}
Explanation: The function returns an empty object because the `keys` array is empty.
Example 4:
Input: obj = null, keys = ["name", "age"]
Output: {}
Explanation: The function returns an empty object because the input object is null.
Constraints
- The input object
objcan contain any data types as values. - The
keysarray will contain only strings. - The function should execute in O(n) time complexity, where n is the length of the
keysarray. - The function should be written in standard TypeScript.
Notes
Consider using a for...of loop or the forEach method to iterate through the keys array. Remember to check if a key exists in the input object before attempting to access its value. Type safety is crucial; ensure your function is properly typed to prevent runtime errors. Think about how to handle potentially missing keys gracefully.