Answers for "clone a object"

15

clone object in js

var student = {name: "Rahul", age: "16", hobby: "football"};

//using ES6
var studentCopy1 = Object.assign({}, student);
//using spread syntax
var studentCopy2 = {...student}; 
//Fast cloning with data loss
var studentCopy3 = JSON.parse(JSON.stringify(student));
Posted by: Guest on April-29-2020
1

how to clone an object

const first = {'name': 'alka', 'age': 21} 
const another = Object.assign({}, first);
Posted by: Guest on June-14-2020
-1

clone an object javascript

//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;
}
Posted by: Guest on January-28-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language