let a = { x: 10, y: 15 }; let b = Object.assign({}, a); console.log(a); //output: { x: 10, y: 15 } console.log(b); //output: { x: 10, y: 15 } console.log(a === b); //output: false
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);
//recursive deep copy of object function dup(o) { // "string", number, boolean if(typeof(o) != "object") { return o; } // null if(!o) { return o; // null } var r = (o instanceof Array) ? [] : {}; for(var i in o) { if(o.hasOwnProperty(i)) { r[i] = dup(o[i]); } } return r; }