From the course: JavaScript on the Go: Objects
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Merge two or more JavaScript objects into a new object - JavaScript Tutorial
From the course: JavaScript on the Go: Objects
Merge two or more JavaScript objects into a new object
- [narrator[ Have you ever needed to combine two or more objects into one object? For instance, you may have had an object containing properties you wanted to copy into another object so that you can work more efficiently with the data. Thinking back to your address book, let's say you had multiple contacts with the same address and you want to avoid manually repeating and copying that information into all contact objects sharing the same address. Merging objects is a common task you can quickly do in one of two ways, JavaScript's Object.assign method or the spread operator. So let's first talk about the Object.assign method. This method copies the properties and values of one or more objects to a target object. You pass Object.assign the target object first or the object receiving the copied properties and any subsequent arguments are the source objects, containing the properties you want to copy. Here's an example of how…
Contents
-
-
-
Inheritance in JavaScript3m 26s
-
(Locked)
The object literal3m 9s
-
(Locked)
Define an object using a constructor function4m 42s
-
(Locked)
Create objects using an existing object as the prototype2m 58s
-
(Locked)
Merge two or more JavaScript objects into a new object3m 52s
-
(Locked)
Common ways to iterate over objects3m 25s
-
(Locked)
JavaScript classes4m 17s
-
(Locked)
Class-based vs. prototype-based inheritance2m 52s
-
-