Answers for "duplicate object javascript"

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
12

copy object javascript

var x = {myProp: "value"};
var y = Object.assign({}, x);
Posted by: Guest on March-08-2020
4

clone javascript object

let clone = Object.assign({}, objToClone);
Posted by: Guest on February-19-2020
1

how to make a deep copy in javascript

JSON.parse(JSON.stringify(o))
Posted by: Guest on March-01-2020
0

best way to clone an object in javascript

const person = {
    firstName: 'John',
    lastName: 'Doe'
};


// using spread ...
let p1 = {
    ...person
};

// using  Object.assign() method
let p2 = Object.assign({}, person);

// using JSON
let p3 = JSON.parse(JSON.stringify(person));
Posted by: Guest on November-04-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 "duplicate object javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language