Q:

how to deep copy an object in javascript

JSON.parse(JSON.stringify(object))
4
let clone = Object.assign({}, objToClone);
3
let a = { x: 10, y: 15 };
let b = Object.assign({}, a);
console.log(a);
//output: { x: 10, y: 15 }
console.log(b);
//output: { x: 10, y: 15 }
console.log(a === b);
//output: false
2
JSON.parse(JSON.stringify(o))
0
const obj1 = { a: 1, b: 2, c: 3 };
// this converts the object to string so there will be no reference from 
// this first object
const s = JSON.stringify(obj1);

const obj2 = JSON.parse(s);
0

New to Communities?

Join the community