Hone logo
Hone
Problems

Selective Property Extraction from JavaScript Objects

This challenge focuses on extracting specific properties from JavaScript objects. It's a common task in data processing and manipulation, allowing you to create new objects containing only the data you need. Successfully completing this challenge demonstrates proficiency in object iteration and property access in JavaScript.

Problem Description

You are given a JavaScript object and an array of property names. Your task is to create a new object containing only the properties specified in the input array. The new object should maintain the original data types and values of the selected properties. If a property name in the input array does not exist in the original object, it should be omitted from the new object.

What needs to be achieved:

  • Create a function that takes an object and an array of property names as input.
  • Return a new object containing only the properties specified in the input array.
  • Handle cases where a property name does not exist in the original object gracefully (by omitting it).

Key Requirements:

  • The function must be named pickProperties.
  • The function must accept two arguments: obj (the object to extract from) and keys (an array of property names to extract).
  • The function must return a new object. The original object should not be modified.
  • The returned object should only contain the properties specified in the keys array that exist in the original obj.

Expected Behavior:

The function should iterate through the keys array and, for each key, check if the key exists in the obj. If the key exists, the corresponding value from obj should be added to the new object.

Edge Cases to Consider:

  • Empty keys array: Should return an empty object.
  • keys array containing property names that do not exist in obj: These properties should be ignored.
  • obj being null or undefined: Should return an empty object.
  • keys being null or undefined: Should return an empty object.
  • obj containing properties with different data types (string, number, boolean, object, array, etc.). The new object should preserve these data types.

Examples

Example 1:

Input: { a: 1, b: 2, c: 3 }, ['a', 'c']
Output: { a: 1, c: 3 }
Explanation: The function extracts properties 'a' and 'c' from the input object and creates a new object with those properties and their corresponding values.

Example 2:

Input: { name: 'Alice', age: 30, city: 'New York' }, ['name', 'occupation']
Output: { name: 'Alice' }
Explanation: The function extracts the 'name' property because it exists in the input object. The 'occupation' property is omitted because it does not exist.

Example 3:

Input: { x: 10, y: 20, z: 30 }, []
Output: {}
Explanation: An empty keys array results in an empty object being returned.

Example 4:

Input: null, ['a', 'b']
Output: {}
Explanation: Null object input results in an empty object being returned.

Constraints

  • The input object obj can contain any valid JavaScript data types as property values.
  • The input array keys will contain strings representing property names.
  • The number of properties in the input object obj can vary.
  • The length of the keys array can vary.
  • The function should execute in O(n) time complexity, where n is the length of the keys array.

Notes

Consider using a loop to iterate through the keys array. The hasOwnProperty() method can be helpful for checking if a property exists in the object. Remember to create a new object to store the extracted properties; avoid modifying the original object.

Loading editor...
javascript