Deep Merge Objects
MediumUtility Functions20 min4 tests
Implement a mergeDeep function that recursively merges two objects together.
Requirements
- Nested plain objects are merged recursively
- Arrays and other values from the source overwrite the target
- Return a new object (do not mutate either input)
- Properties from the second object take precedence
Example
mergeDeep({ a: { b: 1, c: 2 } }, { a: { b: 10, d: 4 } });
// { a: { b: 10, c: 2, d: 4 } }Hints (4)
- Start by spreading the target into a new object.
- Iterate over the keys of the source object.
- If both values at a key are plain objects, recurse.
- Otherwise, the source value overwrites the target value.
Topics
- Utility Functions
Asked at
Meta · Airbnb · Atlassian
