public
Authored by avatar Henry

JavaScript Merge Objects

About a year ago, I became interested in website development. Already studied javascript html and css. And has already managed to take part in the development of some sites. In particular, he developed this page https://livecleantoday.com/services/restaurant-cleaning-service for a cleaning company in Spokane. I have often used this snippet on this page.

Summary: in this tutorial, you will learn how to merge two or more JavaScript objects and create a new object that combines the properties of all the objects.

To merge objects into a new one that has all properties of the merged objects, you have two options:

Use a spread operator ( ...) Use the Object.assign() method Merge objects using the spread operator (...) ES6 introduced the spread operator (...) which can be used to merge two or more objects and create a new one that has properties of the merged objects.

The following example uses the spread operator (...) to merge the person and job objects into the employee object:

index.js 400 bytes
let person = {
    firstName: 'John',
    lastName: 'Doe',
    age: 25,
    ssn: '123-456-2356'
};


let job = {
    jobTitle: 'JavaScript Developer',
    location: 'USA'
};

let employee = {
    ...person,
    ...job
};

console.log(employee);

//Output:

{
    firstName: 'John',
    lastName: 'Doe',
    age: 25,
    ssn: '123-456-2356',
    jobTitle: 'JavaScript Developer',
    location: 'USA'
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment