Here are 3 different methods for copying objects. Each method has pros and cons, so read through and pick the best for your situation
Object.assign method
Use Object.assign
, which "is used to copy the values of all enumerable own properties from one or more source objects to a target object". This copies both values and functions. At the time of writing this, browser support is good but not perfect, but this is the best method IMO of the three.
const obj1 = {a:1, b:2};
const obj1Copy = Object.assign(obj1)
Spread operator method
Alternatively, you can use the spread operator
to spread from one object into another. Keep in mind that this will copy the values of keys, but if you the value of a key is a memory address (an other nested object or an array) then it will only be a shallow copy.
const obj1 = {a: () => {}, b:2}
const obj1Copy = { ...obj1 }
JSON stringify/parse trick
If the object doesn't have any circular references or functions as values, you can use the json stringify trick:
let myCopy = JSON.parse(JSON.stringify(myObject));
No libraries required, and works very well for most objects.
JSON.parse(JSON.stringify(...))
hack. – Naominaorthis
atjQuery.extend(true, {}, this)
? – Salta