DEV Community

loading...

Deep Cloning (JSON.parse + JSON.stringify)

318097 profile image Mehul Lakhanpal Originally published at codedrops.tech ・1 min read
const obj = {
  name: "Joe",
  address: { city: "X" },
};

const clone = JSON.parse(JSON.stringify(obj));
Enter fullscreen mode Exit fullscreen mode

Copying using spread operator or Object.assign() makes a shallow copy (i.e, nested arrays & objects reference is copied instead of their values) whereas, stringify + parse does a deep copy.


Thanks for reading 💙

Follow @codedrops.tech for daily posts.

Instagram ● Twitter ● Facebook

Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript

codedrops.tech

Discussion (0)

pic
Editor guide