How to Merge Javascript Objects
A Javascript Object – now just called JSON
– is a collection of values and properties stored as a map.
How would you merge two or more Javascript objects together thought?
In ECMAScript 2018, the Object Spread
operator will do the trick for you in no time, let’s try it out.
The result of this would be to combine all the elements and override any newer ones where applicable.
An alternative to using the Spread operator
is to use Object.assign
as follows:
|
|
This results in exactly the same output.