javascript clone obj code example
Example 1: clone object in js
var student = {name: "Rahul", age: "16", hobby: "football"};
var studentCopy1 = Object.assign({}, student);
var studentCopy2 = {...student};
var studentCopy3 = JSON.parse(JSON.stringify(student));
Example 2: best way to clone an object in javascript
const person = {
firstName: 'John',
lastName: 'Doe'
};
let p1 = {
...person
};
let p2 = Object.assign({}, person);
let p3 = JSON.parse(JSON.stringify(person));
Example 3: object clone javascript
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target);
console.log(returnedTarget);