JavaScript Interview: Implement deepOmit() Without Mutating the Original Object
Problem Statement
Implement deepOmit(val, keys) to remove specified keys from an object, including nested objects and arrays, without modifying the original object.
For example, if we want to remove b and e, the function should remove them wherever they appear in the nested structure.
Input
const data = {
a: 1,
b: 2,
c: {
d: 3,
e: 4
},
f: [5, 6]
};deepOmit(data, ["b", "e"]);
Expected Output
{
a: 1,
c: {
d: 3
},
f: [5, 6]
}The original data object must remain unchanged.
Solution
export default function deepOmit(val, keys) {
// Handle primitives and null
if (val === null || typeof val !== "object") {
return val;
}
// Handle arrays
if (Array.isArray(val)) {
return val.map(item => deepOmit(item, keys));
}
// Create a new object
const result = {};
// Iterate through the object's keys
for (const key of Object.keys(val)) {
// Skip keys that should be omitted
if (!keys.includes(key)) {
// Recursively process the value
result[key] = deepOmit(val[key], keys);
}
}
return result;
}Understanding the Approach
The key idea is to create a new structure instead of modifying the original one.
1. Handle primitives and null
if (val === null || typeof val !== "object") {
return val;
}If the value is a number, string, boolean, undefined, or null, return it directly.
For example, deepOmit(5, keys) returns 5.
We check null separately because JavaScript considers typeof null to be "object".
2. Handle arrays
if (Array.isArray(val)) {
return val.map(item => deepOmit(item, keys));
}If the value is an array, use map() to create a new array.
Each element is passed through deepOmit() so that nested objects inside arrays are also processed.
For example:
[1, { a: 2, b: 3 }]With keys ["b"], the result becomes:
[1, { a: 2 }]3. Create a new object
const result = {};This creates an empty object to store the properties we want to keep.
We do not use delete because that would modify the original object.
4. Iterate through the properties
for (const key of Object.keys(val)) {Object.keys(val) returns the object's own enumerable property names.
For the input object, the keys are:
["a", "b", "c", "f"]
The loop processes each key one by one.
5. Skip unwanted keys
if (!keys.includes(key)) {keys.includes(key) checks whether the current property exists in the omit list.
- If it exists, skip the property.
- If it does not exist, copy the property into the new object.
For example, when key is "b", the condition is false, so b is not copied.
6. Recursively process each value
result[key] = deepOmit(val[key], keys);
This is the most important line.
It copies the property’s value into the new object, but first passes that value through deepOmit().
If the value is a primitive, it returns directly. If it is an array or object, the function processes it recursively.
This ensures that unwanted keys are removed at every nesting level.
7. Return the new object
return result;
Once all properties have been processed, return the newly created object.
The original object remains unchanged.
Dry Run
Let’s walk through the complete execution.
Input:
const data = {
a: 1,
b: 2,
c: {
d: 3,
e: 4
},
f: [5, 6]
};deepOmit(data, ["b", "e"]);
Step 1: Start with the root object
The function receives data and ["b", "e"].
The value is an object, not null or a primitive, and it is not an array.
Therefore, we create a new empty object:
result = {};Step 2: Process a
The current key is "a".
keys.includes("a"); // falseSince a is not in the omit list, we execute:
result["a"] = deepOmit(1, keys);
The recursive call receives 1. Since it is a primitive, it returns 1.
Now:
result = { a: 1 };Step 3: Process b
The current key is "b".
keys.includes("b"); // trueBecause b is in the omit list, the if block is skipped.
No property is added to result.
result = { a: 1 };Notice that we did not delete b from data. We simply did not copy it.
Step 4: Process c
The current key is "c".
Since c is not in the omit list, we execute:
result["c"] = deepOmit(data["c"], keys);
A recursive call starts for the nested object:
{
d: 3,
e: 4
}This call creates another empty object:
result = {};Inside this recursive call:
- d is not in the omit list, so d: 3 is copied.
- e is in the omit list, so it is skipped.
The nested call returns:
{ d: 3 }That returned object is assigned to the outer object’s c property.
The outer result is now:
{
a: 1,
c: {
d: 3
}
}Step 5: Process f
The current key is "f".
Since f is not in the omit list, we execute:
result["f"] = deepOmit(data["f"], keys);
The value is an array:
[5, 6]
The array condition is true, so this line executes:
return val.map(item => deepOmit(item, keys));
map() processes both elements:
- 5 is a primitive, so it returns 5.
- 6 is a primitive, so it returns 6.
A new array is returned:
[5, 6]
The outer result becomes:
{
a: 1,
c: {
d: 3
},
f: [5, 6]
}Step 6: Return the final result
The loop has finished, so the function executes:
return result;
Final output:
{
a: 1,
c: {
d: 3
},
f: [5, 6]
}The original data object still contains b and c.e.
Why Does This Not Mutate the Original Object?
We never modify val directly.
Instead, we create:
- A new object for every object encountered.
- A new array for every array encountered.
- References to primitive values, which are immutable.
Therefore, changes to the returned structure do not affect the original nested objects or arrays.
This is a recursive, non-mutating transformation.
Complexity Analysis
Let N be the total number of values and properties visited, and K be the number of keys to omit.
- Time complexity: (O(N \times K)) in the worst case, because keys.includes() may scan up to K keys for each property.
- Space complexity: (O(N)) for the newly created objects and arrays, plus the recursion stack.
For better lookup performance with a large omit list, we can convert keys into a Set, making key membership checks average (O(1)).
Interview Takeaways
- Use recursion to process nested objects.
- Use Array.isArray() to preserve arrays as arrays.
- Check for null before treating a value as an object.
- Avoid delete when the original input must remain unchanged.
- Create new objects and arrays to preserve immutability.
Remember: The main idea is to build a new structure containing only the properties you want to keep, recursively processing every nested value.
JavaScript Interview: Implement deepOmit() Without Mutating the Original Object was originally published in Modern Frontend on Medium, where people are continuing the conversation by highlighting and responding to this story.