How to Copy an Object in JavaScript
I recently found out that you can't simply assign an object to another variable to copy it. The two variables will end up pointing to the same object! Fortunately, there are many ways to create a new copy of an object in JavaScript. Here are the ways I found:
The first way I learned was the
Object.assign()method. Learn more about it here.const object1 = { name: 'John' }const object2 = Object.assign({}, object1)object2.name = 'Scott'console.log(object1.name) // Johnconsole.log(object2.name) // ScottThe second way is similar to the first but uses the ES6 spread operator. Learn more about it here.
const object1 = { name: 'John' }const object2 = { ...object1 }object2.name = 'Scott'console.log(object1.name) // Johnconsole.log(object2.name) // ScottLastly, you can use libraries like Underscore or Lodash for the clone method. Learn more about it here
const object1 = { name: 'John' }const object2 = _.clone(object1)object2.name = 'Scott'console.log(object1.name) // Johnconsole.log(object2.name) // Scott
I hope these help you learn how to create newly copied objects without affecting your existing object.