JavaScript Program to Merge Property of Two Objects
function mergeObjects(obj1, obj2) {
return Object.assign({}, obj1, obj2);
}
const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3, d: 4 };
const mergedObj = mergeObjects(obj1, obj2);
console.log(mergedObj); // Output: { a: 1, b: 2, c: 3, d: 4 }
The Object.assign()
method is used to merge the properties of two or more objects into a single object. The first argument to the method is the target object, to which the properties of other objects will be merged. In this case, an empty object is used as the target object, so that the properties of obj1
and obj2
are merged without modifying the original objects.