Register to get access to free programming courses with interactive exercises

Merging JS: Objects

Merge is an operation for combining objects allowing you to pass data from one object to another.

Merge is often used when working with web forms. For example, when a user changes their personal data in the account settings, the data that has been changed is sent to the application as an object. Now we need to move this data from the initial object to the user object. This is how the user is updated:

// This user exists in the system
const user = { name: 'Tirion', email: '', age: 44 };

// Data from form
const data = { name: 'Tirion 2', age: 33 };

// The result should be
// { name: 'Tirion 2', email: '', age: 33 };

The solution is to move each property by hand: =;
user.age = data.age;

// Somewhere here we save the user in the database

Direct assign works well when the data is small and its structure does not change. If there is a lot of data or different data can come in at different points, it can turn into a pile of duplicate code:

if (Object.hasOwn(data, 'name')) { =;

// So we need to list all the possible properties

Using merge, we can reduce this to a single line:

Object.assign(user, data);
// => { name: 'Tirion 2', email: '', age: 33 };

The Object.assign() method takes the object from the first parameter and fills it with the content from the objects in the following parameters. In our case, we have just one source object in the second parameter.

Merging works as follows. If a property was only in the first object, it will stay unchanged. If the property is present in the second (third, fourth etc.) object, it is written to the first object no matter if it was there or not. Therefore, if the property was present in both the first object and the second object, it will be overwritten by the value from the second object:

const obj1 = { a: 'a', b: 'b' };
const obj2 = { c: 'c', b: 'v' };
Object.assign(obj1, obj2);
// => { a: 'a', b: 'v', c: 'c' }

The Object.assign() method has one limitation, it only carries out a shallow merge. Nested objects are not compared, but simply replaced:

const obj1 = { a: { a: 1 } }
const obj2 = { a: { b: 1 } }
Object.assign(obj1, obj2);
// => { a: { b: 1 } }

Like any other powerful mechanism, merging needs to be used carefully. There are fields in objects that should not be overwritten when merging, such as the amount of money in the user's account. If you do not control the second object's data structure, important properties may end up being rewritten (accidentally or maliciously) if properties are allowed in that shouldn't be.

When it comes to web forms, it's technically always possible to send more data than the form expects.

Recommended materials

  1. has() function from the Lodash library

Hexlet Experts

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
hours of theory

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs
Development of front-end components for web applications
10 months
from scratch
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.