how to create an object copy js code example
Example 1: copy object javascript
// es6
const obj = {name: 'john', surname: 'smith'};
const objCopy = {...obj};
Example 2: 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));