Skip to the solution
MediumDSAAsked in 20247 min read

Implement omit(obj, paths) Supporting Both Shallow and Nested Paths

3.3k views

The problem

Problem, as an interviewer would state it: "Implement omit(obj, paths) — building a genuinely NEW object with every property EXCEPT the specified paths (both shallow keys and nested dot-paths) removed, matching lodash's own well-known _.omit behavior extended to support nested paths."

Examples:

Code
omit({a:1, b:2, c:{d:3, e:4}}, ["b", "c.e"]); // {a:1, c:{d:3}}

Clarifying questions expected:

  • Does this need to support removing a NESTED field specifically (leaving its sibling fields intact), not just entire top-level keys?
  • Should the original object remain completely untouched, including its nested sub-objects?
  • What is the real, correct behavior if a specified path does not actually exist in the object at all?

Code / implementation expected: Yes — real, direct proof of correct shallow-key removal, correct nested-field removal (with sibling fields preserved), and confirmation the original object is never mutated.

  • object
  • recursion

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: omit() correctly removes both shallow keys and nested fields, confirming sibling fields at the same nested level are genuinely preserved

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 58 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Index By Utility

Continue