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:
obj: The original JavaScript object from which properties will be omitted.keysToOmit: An array of strings, where each string represents the name of a property to be omitted fromobj.
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
keysToOmitmust be absent from the returned object. - All other properties from the original
objmust be present in the returned object. - The function should handle cases where
keysToOmitmight contain keys that are not present inobj. These should simply be ignored. - The original object
objmust 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
objis empty, the function should return an empty object. - Empty
keysToOmitArray: IfkeysToOmitis empty, the function should return a shallow copy of the original object. keysToOmitwith Non-existent Keys: IfkeysToOmitcontains keys that are not present inobj, 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
objwill always be a valid JavaScript object. - The input
keysToOmitwill always be a valid JavaScript array of strings. - The number of properties in
objwill be between 0 and 1000. - The number of keys in
keysToOmitwill 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
keysToOmitcan 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.