[Javascript] structuredClone - deep clone object

const calendarEvent = {
  title: 'abc submit',
  date: new Date(123),
  attendees: ["Steve", {name: 'Steve'}]
}

const copied = structuredClone(calendarEvent)

copied.attendees.push('Zwan')
copied.attendees[1].name = 'Zwan'

console.log(calendarEvent, copied)

/*
{
  "title": "abc submit",
  "date": "1970-01-01T00:00:00.123Z",
  "attendees": [
    "Steve",
    {
      "name": "Steve"
    }
  ]
},  {
  "title": "abc submit",
  "date": "1970-01-01T00:00:00.123Z",
  "attendees": [
    "Steve",
    {
      "name": "Zwan"
    },
    "Zwan"
  ]
} 
*/

 

posted @ 2023-03-16 15:18  Zhentiw  阅读(29)  评论(0编辑  收藏  举报