0

I am new to node and JavaScript. While going through a repository I found a snippet

const x = { ...JSON.parse(JSON.stringify(obj1)) };

I know that JSON.parse(JSON.stringify()) is used for cloning an object but what is the need of spread operator here?

Areeba Akhtar
  • 137
  • 10
  • 4
    I am bamboozled by this use. `JSON.parse(JSON.stringify(obj))` already deep copies the object (with some caveats), not sure why would someone want to further create a shallow copy of already deep copied object – Kanishk Anand Nov 20 '21 at 09:31
  • Is this from a "big" library, or from an custom one (e.g. tiny project, in-house utility library)? In the latter case I would classify this as something of an oversight or as not-thought-through, but something that never caught anyone's eye because it's not a bug. – Tomalak Nov 20 '21 at 09:39

1 Answers1

0

Never mind. That brackets are redundant, the parse/stringify pair is enough

const x = JSON.parse(JSON.stringify(obj1));
Max
  • 1,824
  • 13
  • 22