Deep merge Objects in Javascript with Mergerino

artydev

artydev

Posted on December 16, 2020

Deep merge Objects in Javascript with Mergerino

To merge objects in JS, you can use Object.assign.
The problem with is that it only accomplishes 'shallow merge'.

It does not take in account nested properties.

In my scripts I use Mergerino to get things done.

You can test it here : Merge

const merge = mergerino

const user = {
  name: 'David',
  phone: 122345678,
  location: {
    city: 'Camden',
    country: 'UK'
  }
};
const updates = {
  location: {
    city: 'Smithfield'
  }
};
console.log(Object.assign({}, user, updates));

console.log(merge(user, updates))
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
artydev
artydev

Posted on December 16, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related