Deep Merge Two Objects

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write deepMerge(target, source), where both are plain objects, for layering settings such as a user's preferences over the defaults. Return a new object with the keys of both. When both hold a plain object under the same key, merge those two the same way, at any depth. Otherwise the source's value wins, and that includes arrays: a source array replaces the target's array rather than being combined with it. A source value of undefined is ignored, as if source didn't have that key. Change neither input, and share no plain objects or arrays with them, so changing the result at any depth leaves target and source as they were.

Your code must pass

  • Merges nested objects key by key
  • Lets the source win for other values
  • Replaces arrays rather than merging them
  • Ignores undefined in the source
  • Leaves both inputs unchanged, even after the result changes