<?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: aram1l7</title>
    <description>The latest articles on DEV Community by aram1l7 (@aram1l7).</description>
    <link>https://dev.to/aram1l7</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%2F971198%2F09c4ce50-9bfd-4290-8bcb-945eb10d0515.jpeg</url>
      <title>DEV Community: aram1l7</title>
      <link>https://dev.to/aram1l7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aram1l7"/>
    <language>en</language>
    <item>
      <title>Copying objects in javascript</title>
      <dc:creator>aram1l7</dc:creator>
      <pubDate>Sun, 13 Nov 2022 18:23:27 +0000</pubDate>
      <link>https://dev.to/aram1l7/copying-objects-in-javascript-1gcf</link>
      <guid>https://dev.to/aram1l7/copying-objects-in-javascript-1gcf</guid>
      <description>&lt;p&gt;There are a few ways to copy objects in javascript. One of them is using one of the native javascript object methods - Object.assign&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = {
 name:"John",
 surname:"Doe",
 age:15
}

const copyObj = Object.assign({}, obj);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With ES6, you can also use the spread operator as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = {
  name:"John",
  surname:"Doe",
  age:15
}

const copyObj = {...obj}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, these methods are for creating shallow copies.&lt;br&gt;
Let's say you have a nested object and you use one of the methods mentioned above this is what will happen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = {
  name:"John",
  surname:"Doe",
  age:19,
  location:{
    country:"USA",
    city:"New York",
    address:"Queens, New York 11418, USA",
      }
 }

const copyObj = {...obj}

obj.location.country = "Russia"
console.log(copyObj.location.country) //Russia
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The nested objects will be copied as a reference, that means when you change some property of the nested object, it will also affect the copied object.&lt;br&gt;
There are several ways to make a copy of nested object, one of them is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = {
  name:"John",
  surname:"Doe",
  age:19,
  location:{
    country:"USA",
    city:"New York",
    address:"Queens, New York 11418, USA",
      }
 }

const copyObj = JSON.parse(JSON.stringify(obj));
obj.location.country = "Russia"
console.log(copyObj.location.country) //USA
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or using lodash's cloneDeep utility function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import cloneDeep from 'lodash/cloneDeep'

const obj = {
  name:"John",
  surname:"Doe",
  age:19,
  location:{
    country:"USA",
    city:"New York",
    address:"Queens, New York 11418, USA",
   }
 }

const copyObj = cloneDeep(obj)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can find the original article here:&lt;br&gt;
&lt;a href="https://thejsblog.netlify.app/posts/how-to-copy-objects-in-javascript"&gt;Copying objects in javascript&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
