Skip to content
Deep실무 경험

My experience with deep copy

1 min readWorkMoved from my Tistory blogRead the Korean original

At work, I recently built carInspection, that is, a feature that lets sellers register and edit things like a vehicle's repair history.

A carInspection was registered through a button in a popup. If a seller pressed the button to register a carInspection, then reopened the window after registering and canceled partway through editing, the carInspection information registered earlier had to stay as it was.

To build this, when the register button was pressed, I also assigned the same information registered in the original object to duplicatedConditionList. But if I passed the original object as is, duplicatedConditionList would change too during editing, so I had to use a complete deep copy.

The implementation was simple, as shown below.

js
const duplicatedCoditionList = JSON.parse(JSON.stringify(coditionList));

When the cancel button was pressed, I did the reverse and assigned the copied object to the variable holding the original object, which solved it easily.

  • #deep copy
  • #JSON.parse()
  • #JSON.stringify()

More in Work