Hone logo
Hone
Problems

Selective Object Property Removal in JavaScript

This challenge focuses on creating a function that selectively removes properties from a JavaScript object based on a provided list of keys. This is a common task in data processing and API interactions where you might need to sanitize or filter objects before sending them or storing them. Successfully completing this challenge demonstrates understanding of object manipulation and array methods in JavaScript.

Problem Description

You are tasked with writing a JavaScript function called omitProperties that takes two arguments: an object (obj) and an array of strings (keysToOmit). The function should return a new object that is a copy of the original object, but with the properties specified in keysToOmit removed. The original object should not be modified.

Key Requirements:

  • Immutability: The function must return a new object. Modifying the original object is not acceptable.
  • Property Removal: Properties whose keys are present in the keysToOmit array should be absent in the returned object.
  • Key Type: keysToOmit will contain strings representing the keys to omit.
  • Handles Missing Keys: If a key in keysToOmit does not exist in the original object, the function should gracefully ignore it and continue processing.
  • Handles Empty Input: If keysToOmit is an empty array, the function should return a copy of the original object.

Expected Behavior:

The function should iterate through the keysToOmit array and, for each key, check if it exists in the input object. If it does, the property should be removed from the new object being constructed. The function should return this new, modified object.

Edge Cases to Consider:

  • What happens if obj is null or undefined? (Assume it will always be a valid object for this challenge, but good to consider).
  • What happens if keysToOmit contains duplicate keys? (The function should only remove the property once).
  • What happens if keysToOmit contains keys that are not strings? (Assume all keys will be strings for this challenge).

Examples

Example 1:

Input:
obj = { a: 1, b: 2, c: 3, d: 4 };
keysToOmit = ['b', 'd'];

Output:
{ a: 1, c: 3 }

Explanation: The properties 'b' and 'd' are removed from a copy of the original object, resulting in a new object with only 'a' and 'c'.

Example 2:

Input:
obj = { name: 'Alice', age: 30, city: 'New York' };
keysToOmit = [];

Output:
{ name: 'Alice', age: 30, city: 'New York' }

Explanation: Since `keysToOmit` is empty, a copy of the original object is returned without any modifications.

Example 3:

Input:
obj = { x: 10, y: 20, z: 30 };
keysToOmit = ['a', 'b', 'x'];

Output:
{ y: 20, z: 30 }

Explanation: The property 'x' is removed. 'a' and 'b' are ignored because they don't exist in the original object.

Constraints

  • obj will always be a valid JavaScript object.
  • keysToOmit will be an array of strings.
  • The function must have a time complexity of O(n*m) where n is the number of keys to omit and m is the number of keys in the object. While more optimized solutions exist, this constraint is acceptable for this challenge.
  • The function must not modify the original object.

Notes

Consider using the spread operator (...) or Object.assign() to create a shallow copy of the object. Iterating through the keysToOmit array and checking for the existence of each key in the object is a straightforward approach. Remember to create a new object to avoid modifying the original.

Loading editor...
javascript