mergeObjects

Easy · Object Manipulation · 2 test cases

Given two objects, merge them into a single new object. If a key exists in both objects, the value from the second object should be used.

Examples

mergeObjects({a: 1, b: 2}, {c: 3, d: 4}) → {a: 1, b: 2, c: 3, d: 4}
mergeObjects({a: 1, b: 2}, {b: 3, c: 4}) → {a: 1, b: 3, c: 4}

Starter code

function mergeObjects(obj1, obj2) {

}

Complexity of the optimal solution

Time O(n + m), space O(n + m). The spread syntax creates a new object and copies the properties from both input objects. The time and space required are proportional to the total number of properties in both objects (n + m).

Solve mergeObjects in the browser