AniUI Academy
hard+350 XPPractice

Deep merge two configuration objects

Defaults, then workspace settings, then per-user overrides. With Object.assign a user who sets one nested option erases every other option in that branch, because the shallow copy replaces the whole sub-object.

Implement deepMerge(base, patch). Return a new object containing every key from base, with the keys from patch applied on top. Where both sides hold a plain object for the same key, merge those recursively. Where they do not, the value from patch replaces the one from base outright — so an array in patch replaces an array in base rather than concatenating with it, and a primitive in patch replaces an object in base. A key whose value in patch is undefined is skipped entirely and the value from base survives. A value of null is a real value and does overwrite. Neither input is modified.

What it has to do

  • Recursively merge keys where both sides hold a plain object.
  • Let the patch value replace the base value in every other case.
  • Replace arrays outright rather than concatenating them.
  • Skip patch keys whose value is undefined, but let null overwrite.
  • Do not mutate either input object.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.