js object clone 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: how to make a deep copy in javascript
JSON.parse(JSON.stringify(o))
Example 4: how to clone an object
const first = {'name': 'alka', 'age': 21}
const another = Object.assign({}, first);
Example 5: javascript clone object
let clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
Example 6: 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));