Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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));
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #clone #object #javascript
ADD COMMENT
Topic
Name
9+5 =