Selective Object Property Extraction
Imagine you're working with complex JavaScript objects, perhaps from an API response or a configuration file. Often, you only need a subset of the available properties to perform a specific task. Manually creating a new object by copying each desired property can be tedious and error-prone. This challenge will help you master a concise and efficient way to extract specific properties from an object.
Problem Description
Your task is to create a JavaScript function that takes an object and a list of property keys as input. The function should return a new object containing only the properties specified in the list of keys.
Key Requirements:
- The function should accept two arguments:
obj: The source object from which to extract properties.keys: An array of strings, where each string represents a property key to be extracted.
- The function must return a new object. It should not modify the original
obj. - If a key in the
keysarray does not exist in theobj, it should be ignored and not cause an error. - The order of properties in the returned object does not matter.
Expected Behavior:
The function should iterate through the provided keys. For each key, it should check if that key exists in the obj. If it exists, the property (key-value pair) should be copied to the new object.
Edge Cases to Consider:
- The input
objmight be an empty object. - The input
keysarray might be empty. - The
keysarray might contain duplicate keys (though the resulting object will only have unique properties). - The
keysarray might contain keys that do not exist in theobj.
Examples
Example 1:
Input:
obj = { name: "Alice", age: 30, city: "New York", occupation: "Engineer" }
keys = ["name", "city"]
Output:
{ name: "Alice", city: "New York" }
Explanation:
The function extracts the 'name' and 'city' properties from the input object.
Example 2:
Input:
obj = { id: 1, title: "Book", author: "Author Name" }
keys = ["id", "publisher"]
Output:
{ id: 1 }
Explanation:
The function extracts the 'id' property. The 'publisher' key is not found in the object, so it's ignored.
Example 3:
Input:
obj = {}
keys = ["name", "age"]
Output:
{}
Explanation:
The input object is empty, so no properties can be extracted, even though keys are provided.
Example 4:
Input:
obj = { a: 1, b: 2, c: 3 }
keys = []
Output:
{}
Explanation:
The keys array is empty, so no properties are requested for extraction.
Constraints
- The input
objwill be a valid JavaScript object. - The input
keyswill be a valid JavaScript array of strings. - The number of properties in
objwill be between 0 and 1000. - The number of keys in the
keysarray will be between 0 and 1000. - Property values can be of any valid JavaScript data type.
- The solution should aim for reasonable performance, avoiding excessive iteration or deeply nested operations.
Notes
Consider common JavaScript object manipulation techniques. Think about how you can efficiently iterate through the keys and check for their existence in the obj. There are several idiomatic ways to achieve this in JavaScript.