JSON.parse(JSON.stringify(object))
//Using JSON stringify function
var obj2 = JSON.parse(JSON.stringify(obj));
//Using lodash deep clone method
var obj2 = _.cloneDeep(obj, true);
//Angular framework comes with angular.copy function
var obj2 = angular.copy(obj);
// Using jQuery extend function
var obj2 = $.extend(true, {}, obj);
// Create an object with a value and a circular reference to itself.
const original = { name: "MDN" };
original.itself = original;
// Clone it
const clone = structuredClone(original);
console.assert(clone !== original); // the objects are not the same (not same identity)
console.assert(clone.name === "MDN"); // they do have the same values
console.assert(clone.itself === clone); // and the circular reference is preserved
// NORMAL COPY---------------------------------------
const a = { x: 0}
const b = a;
b.x = 1; // also updates a.x
// SHALLOW COPY---------------------------------------
const a = { x: 0, y: { z: 0 } };
const b = {...a}; // or const b = Object.assign({}, a);
b.x = 1; // doesn't update a.x
b.y.z = 1; // also updates a.y.z
// DEEP COPY---------------------------------------
const a = { x: 0, y: { z: 0 } };
const b = JSON.parse(JSON.stringify(a));
b.y.z = 1; // doesn't update a.y.z
const obj1 = { a: 1, b: 2, c: 3 };
// this converts the object to string so there will be no reference from
// this first object
const s = JSON.stringify(obj1);
const obj2 = JSON.parse(s);