Deep Omit
JavaScript
medium
30 mins
Implement a function deepOmit(obj, keysToOmit) that removes all occurrences of specific keys from an object or array, regardless of how deeply nested they are.
- It should not mutate the original object.
- The function should recursively traverse objects and arrays and omit the given keys.
Input:
obj: A deeply nested object or array.keysToOmit: An array of strings representing the keys to be removed.
Output:
- A new object/array structure with the specified keys omitted at all levels.
Example Inputs & Outputs
deepOmit({ a: 1, b: { c: 2, d: 3 } }, ['c']); // Output: { a: 1, b: { d: 3 } } deepOmit({ a: { b: { c: 3, d: 4 }, c: 5 }, c: 1 }, ['c']); // Output: { a: { b: { d: 4 } } } deepOmit([{ a: 1, b: 2 }, { b: 3, c: 4 }], ['b']); // Output: [{ a: 1 }, { c: 4 }]
Constraints & Edge Cases
- The object may contain nested arrays or other objects.
- Keys may appear multiple times at different nesting levels.
- Arrays should retain their structure.
- Must not mutate the original input object or array.
Companies:
airbnb
salesforce
swiggy
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
