how to clone object in javascript 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: clone javascript object
let clone = Object.assign({}, objToClone);
Example 3: javascript clone object
var x = {myProp: "value"};
var xClone = Object.assign({}, x);
Example 4: javascript clone object
let clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
Example 5: 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 6: javascript clone object
const me = {
name: 'Jakes',
age: 30,
};
const friend = {...me};
friend.age = 27;
console.log(friend.age);
console.log(me.age);
const me = {
name: 'Jonas',
age: 30,
};
const friend = me;
friend.age = 27;
console.log(friend.age);
console.log(me.age);