create a deep copy of an object javascript code example
Example 1: deep clone object javascript
JSON.parse(JSON.stringify(object))
Example 2: how to make a deep copy in javascript
JSON.parse(JSON.stringify(o))
Example 3: make copy of object javascript
var x = {key: 'value'}
var y = JSON.parse(JSON.stringify(x))
Example 4: how to deep copy an object in javascript
const obj1 = { a: 1, b: 2, c: 3 };
const s = JSON.stringify(obj1);
const obj2 = JSON.parse(s);
Example 5: deep copy js
function dup(o) {
if(typeof(o) != "object") {
return o;
}
if(!o) {
return o;
}
var r = (o instanceof Array) ? [] : {};
for(var i in o) {
if(o.hasOwnProperty(i)) {
r[i] = dup(o[i]);
}
}
return r;
}