Fraser Hamilton

How to merge two objects in JavaScript

June 15, 2020

Thanks to the introduction of the spread operator we can easily merge two objects into one in Javascript. Using the following method:

const firstObject = {
  make: 'Land Rover',
  model: 'Discovery',
}

const secondObject = {
  color: 'Black',
}

const mergedObject = { ...firstObject, ...secondObject }

Be careful when merging objects that share attribute names as the second will overwrite the first.


Written by Fraser Hamilton a full stack developer based out of Edinburgh, Scotland.

© 2020, Fraser Hamilton