0
Q:

how to clone an object

var x = {myProp: "value"};
var xClone = Object.assign({}, x);

//Obs: nested objects are still copied as reference.
1
var sheep={"height":20,"name":"Melvin"};
var clonedSheep=JSON.parse(JSON.stringify(sheep));

//note: cloning like this will not work with some complex objects such as:  Date(), undefined, Infinity
// For complex objects try: lodash's cloneDeep() method or angularJS angular.copy() method
0
const first = {'name': 'alka', 'age': 21} 
const another = Object.assign({}, first);
0
//returns a copy of the object
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;
}
-1

New to Communities?

Join the community