Description
Sometimes, you need to remove certain keys from an object, including nested objects or arrays, in a clean and recursive way.
The deepOmit function should:
- Accept an object (or array) and an array of keys to remove.
- Recursively traverse all nested objects and arrays.
- Return a new object or array with the specified keys removed at all levels.
- Leave all other values intact.
This is useful for filtering sensitive data, cleaning API responses, or simplifying deeply nested structures.
Your Task
Write a function deepOmit(obj, keys) that:
- Takes an input object or array
obj. - Takes an array of string keys
keysto remove. - Returns a new object/array with the specified keys omitted recursively.
Example
deepOmit({ a: 1, b: 2, c: 3 }, ['b']);
// Output: { a: 1, c: 3 }
const obj = {
a: 1,
b: 2,
c: {
d: 3,
e: 4,
},
f: [5, 6],
};
deepOmit(obj, ['b', 'c', 'e']);
// Output: { a: 1, f: [5, 6] }Hints / How to Think About It
- Use recursion to handle nested objects and arrays.
- Check if a value is a plain object before iterating keys.
- For arrays, map over each item and recursively call
deepOmit. - Skip any key that exists in the
keysarray. - Always return a new object/array to avoid mutating the input.
Think of this like “filtering out unwanted keys at all levels” of your data structure.