What is the method to combine properties from two JavaScript objects?

I am looking for a way to blend two simple, flat JavaScript objects dynamically during runtime. For instance, suppose I have one object representing meal details and another containing pet information. I’d like to merge the properties so that one object holds all the key-value pairs without involving recursion or merging of functions. Below is an example of an alternative approach:

let details1 = { dish: 'sushi', vehicle: 'tesla' };
let details2 = { pet: 'cat' };

function combineAttributes(target, source) {
  Object.keys(source).forEach(function(key) {
    target[key] = source[key];
  });
  return target;
}

combineAttributes(details1, details2);
// Now 'details1' contains dish, vehicle, and pet properties

Is there a native function available to perform this merge?

I have used Object.assign quite extensively and it is indeed the native method to combine properties of two objects. In my projects, I used Object.assign to merge objects by simply passing in the source objects and letting JavaScript handle the assignment of enumerable properties into the target object. This approach is very efficient for combining flat structures and simplifies the code by removing the need to loop over properties manually. It is important to note though that if two objects have the same property name, the value in the later object will override the earlier one.