Hone logo
Hone
Problems

Object Property Omission in JavaScript

Given an object and a list of properties to omit, your task is to create a new object that contains all the properties of the original object except for those specified for omission. This is a common operation when you need to selectively share data, such as removing sensitive information before sending it to a client or cleaning up an object for display.

Problem Description

You need to write a JavaScript function that accepts two arguments:

  1. obj: The original JavaScript object from which properties will be omitted.
  2. keysToOmit: An array of strings, where each string represents the name of a property to be omitted from obj.

The function should return a new object that is a shallow copy of obj, but without any of the properties whose keys are present in keysToOmit. The original object obj should remain unchanged.

Key Requirements:

  • The function must return a new object.
  • Properties listed in keysToOmit must be absent from the returned object.
  • All other properties from the original obj must be present in the returned object.
  • The function should handle cases where keysToOmit might contain keys that are not present in obj. These should simply be ignored.
  • The original object obj must not be modified.

Expected Behavior:

The function should iterate through the properties of the input object. For each property, it should check if its key is present in the keysToOmit array. If the key is not in keysToOmit, the property should be copied to the new object.

Edge Cases:

  • Empty Input Object: If obj is empty, the function should return an empty object.
  • Empty keysToOmit Array: If keysToOmit is empty, the function should return a shallow copy of the original object.
  • keysToOmit with Non-existent Keys: If keysToOmit contains keys that are not present in obj, these should be safely ignored.

Examples

Example 1:

Input:
obj = {
  name: "Alice",
  age: 30,
  email: "alice@example.com",
  isAdmin: false
}
keysToOmit = ["email", "isAdmin"]

Output:
{
  name: "Alice",
  age: 30
}

Explanation:
The properties "email" and "isAdmin" were present in keysToOmit, so they are excluded from the new object.

Example 2:

Input:
obj = {
  id: 123,
  title: "Coding Challenge",
  description: "A great exercise"
}
keysToOmit = ["nonexistentKey"]

Output:
{
  id: 123,
  title: "Coding Challenge",
  description: "A great exercise"
}

Explanation:
The key "nonexistentKey" was not present in the original object, so it was effectively ignored. The new object is a shallow copy of the original.

Example 3:

Input:
obj = {}
keysToOmit = ["anyKey"]

Output:
{}

Explanation:
The input object is empty, so the output is also an empty object, regardless of keysToOmit.

Constraints

  • The input obj will always be a valid JavaScript object.
  • The input keysToOmit will always be a valid JavaScript array of strings.
  • The number of properties in obj will be between 0 and 1000.
  • The number of keys in keysToOmit will be between 0 and 100.
  • The solution should be efficient, aiming for a time complexity of O(N*M) in the worst case (where N is the number of properties in the object and M is the number of keys to omit, if using linear search for keys), or O(N) if keysToOmit can be efficiently checked (e.g., using a Set).

Notes

Consider how you can efficiently check if a key is present in the keysToOmit array. For larger arrays, converting keysToOmit into a Set first might offer performance benefits. Remember that this is a shallow copy; nested objects or arrays within the original object will be referenced, not deeply cloned.

Loading editor...
javascript