Merging Objects in JavaScript
Object merging is a common task in JavaScript, particularly when dealing with configuration settings, data aggregation, or updating object properties. This challenge asks you to write a function that efficiently merges two JavaScript objects, prioritizing properties from the second object in case of conflicts. This is a fundamental skill for any JavaScript developer.
Problem Description
You are tasked with creating a JavaScript function called mergeObjects that takes two objects as input and returns a new object containing all the properties of both input objects. If a property exists in both objects, the value from the second object should overwrite the value from the first object. The function should not modify the original objects; it should return a new merged object.
Key Requirements:
- The function must accept two JavaScript objects as arguments.
- The function must return a new object.
- If a key exists in both objects, the value from the second object should be used in the merged object.
- The function should handle objects with nested properties correctly (shallow merge is sufficient for this challenge).
- The function should handle empty objects gracefully.
Expected Behavior:
The function should iterate through the properties of both objects and copy them into the new merged object. When a key is found in both objects, the value from the second object should take precedence.
Edge Cases to Consider:
- One or both input objects are empty.
- Objects contain properties with the same name but different data types.
- Objects contain nested objects (shallow merge is acceptable).
Examples
Example 1:
Input: obj1 = { a: 1, b: 2 }, obj2 = { b: 3, c: 4 }
Output: { a: 1, b: 3, c: 4 }
Explanation: 'b' exists in both objects. The value from obj2 (3) overwrites the value from obj1 (2). 'c' only exists in obj2, so it's added to the merged object.
Example 2:
Input: obj1 = { a: 1, b: { x: 10 } }, obj2 = { b: { y: 20 }, c: 4 }
Output: { a: 1, b: { y: 20 }, c: 4 }
Explanation: 'b' exists in both objects. The value from obj2 (an object with y:20) overwrites the value from obj1 (an object with x:10). This is a shallow merge - the nested objects are replaced, not recursively merged.
Example 3:
Input: obj1 = {}, obj2 = { a: 1, b: 2 }
Output: { a: 1, b: 2 }
Explanation: obj1 is empty. All properties from obj2 are copied to the new object.
Constraints
- The input objects will only contain primitive data types (string, number, boolean, null, undefined) or other objects. No functions or special object types are expected.
- The function must complete within a reasonable time (O(n) where n is the total number of properties in both objects).
- The function must be written in standard JavaScript (ES5 or later).
Notes
Consider using a loop to iterate through the properties of both objects. The Object.keys() method can be helpful for retrieving the keys of an object. Remember to create a new object to store the merged result, avoiding modification of the original input objects. A shallow merge is sufficient; you don't need to recursively merge nested objects.