<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: jatins52</title>
    <description>The latest articles on DEV Community by jatins52 (@jatins52).</description>
    <link>https://dev.to/jatins52</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F733409%2Fd085fec2-bb14-4d84-b4ca-282222ed31d0.png</url>
      <title>DEV Community: jatins52</title>
      <link>https://dev.to/jatins52</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jatins52"/>
    <language>en</language>
    <item>
      <title>How to deep copy a nested object in Javascript</title>
      <dc:creator>jatins52</dc:creator>
      <pubDate>Mon, 19 Aug 2024 15:52:52 +0000</pubDate>
      <link>https://dev.to/jatins52/how-to-deep-copy-a-nested-object-in-javascript-4nmj</link>
      <guid>https://dev.to/jatins52/how-to-deep-copy-a-nested-object-in-javascript-4nmj</guid>
      <description>&lt;p&gt;How to deep copy a nested object in Javascript&lt;/p&gt;

&lt;p&gt;So you have a nested object and let's say you need to copy it to a different variable which is completely different from original one. &lt;/p&gt;

&lt;p&gt;How are we doing it today?&lt;br&gt;
Original object:&lt;br&gt;
&lt;code&gt;const obj1 = {name: 'John Smith', address: {mailing: {line1: 'address line 1', line2: 'address line 2', city:'New York'}}};&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Copied Object:&lt;br&gt;
Method 1:&lt;br&gt;
using spread operator&lt;br&gt;
&lt;code&gt;const obj2 = {...obj1}&lt;/code&gt;;&lt;br&gt;
Method 2:&lt;br&gt;
some people would prefer even expensive operation:&lt;br&gt;
&lt;code&gt;const obj2 = JSON.parse(JSON.stringify(obj1));&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Problem with method 1 is that spread operator copies the object without reference but it won't do it for nested object e.g. for address key of obj1. So changing &lt;code&gt;obj2.address.line1&lt;/code&gt; will change &lt;code&gt;obj1.address.line1&lt;/code&gt; also.&lt;/p&gt;

&lt;p&gt;And method 2 simply looks wrong and expensive.&lt;/p&gt;

&lt;p&gt;So how do we do this?&lt;br&gt;
Answer:&lt;br&gt;
&lt;code&gt;const obj2 = structuredClone(obj1);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And now you have created a deep copy of obj1 and obj1 and obj2 are completely different objects.&lt;/p&gt;

&lt;p&gt;Note: &lt;code&gt;structuredClone()&lt;/code&gt; method is newly available method and might not work in browsers older than March 2022.&lt;/p&gt;

&lt;p&gt;This is my first post. Please comment and connect and let me know what I can make better in my next post.&lt;/p&gt;

&lt;p&gt;Thank you!!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
