Skip to main content

Command Palette

Search for a command to run...

Deep Cloning (JSON.parse + JSON.stringify)

Published
1 min readView as Markdown
const obj = {
  name: "Joe",
  address: { city: "X" },
};

const clone = JSON.parse(JSON.stringify(obj));

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.

More from this blog

Codedrops.tech

49 posts