js copy object ot another code example
Example 1: copy object javascript
const obj = {name: 'john', surname: 'smith'};
const objCopy = {...obj};
Example 2: javascript clone object
var sheep={"height":20,"name":"Melvin"};
var clonedSheep=JSON.parse(JSON.stringify(sheep));
Example 3: clone an object javascript
function clone(obj) {
if (null == obj || "object" != typeof obj) return obj;
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
}
return copy;
}