<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>lodash | Bogdan's blog</title>
	<atom:link href="https://blog.bogdancarpean.com/tag/lodash/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.bogdancarpean.com</link>
	<description>Code and anything else.</description>
	<lastBuildDate>Fri, 24 Jan 2020 18:55:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>

<image>
	<url>https://blog.bogdancarpean.com/wp-content/uploads/2022/12/icons8-pencil-drawing-96.png</url>
	<title>lodash | Bogdan's blog</title>
	<link>https://blog.bogdancarpean.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Deepcopy of JavaScript Objects and Arrays using lodash&#8217;s cloneDeep method</title>
		<link>https://blog.bogdancarpean.com/deepcopy-of-javascript-objects-and-arrays-using-lodashs-clonedeep-method/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=deepcopy-of-javascript-objects-and-arrays-using-lodashs-clonedeep-method</link>
					<comments>https://blog.bogdancarpean.com/deepcopy-of-javascript-objects-and-arrays-using-lodashs-clonedeep-method/#respond</comments>
		
		<dc:creator><![CDATA[Bogdan]]></dc:creator>
		<pubDate>Fri, 12 Jan 2018 14:08:06 +0000</pubDate>
				<category><![CDATA[Angular]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[angular]]></category>
		<category><![CDATA[cloneDeep]]></category>
		<category><![CDATA[copy objects]]></category>
		<category><![CDATA[deep clone]]></category>
		<category><![CDATA[deep copy]]></category>
		<category><![CDATA[lodash]]></category>
		<guid isPermaLink="false">http://blog.bogdancarpean.com/?p=182</guid>

					<description><![CDATA[<p>While developing JavaScript applications, often we need to make copies of Objects or Arrays containing Objects. Then we want to work with those copies without affecting the original objects. In a common scenario, we are going to deal with a simple, one level Object (an Object without other Objects nested inside), like in the following [&#8230;]</p>
The post <a href="https://blog.bogdancarpean.com/deepcopy-of-javascript-objects-and-arrays-using-lodashs-clonedeep-method/">Deepcopy of JavaScript Objects and Arrays using lodash’s cloneDeep method</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></description>
										<content:encoded><![CDATA[<div style="position: relative;">While developing JavaScript applications, often we need to make copies of Objects or Arrays containing Objects. Then we want to work with those copies without affecting the original objects.<br />
In a common scenario, we are going to deal with a simple, one level Object (an Object without other Objects nested inside), like in the following example:</div>
<pre class="EnlighterJSRAW" data-enlighter-language="js">let originalObject = {name: 'Bunt', type: 'dog'};</pre>
<p>Then, we want to make a copy of the <code>originalObject</code>. In <code>ES6</code>, we are going to do something like this:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">let copiedObject = Object.assign({}, originalObject);</pre>
<p>This will create the <code>copiedObject</code> with all the properties and respective values of the <code>originalObject</code>. Now, if we want to change a value in the <code>copiedObject</code>, this will not change also the value into the <code>originalObject:</code></p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">copiedObject.type = 'cat';

console.log(originalObject.type); // will print 'dog'
console.log(copiedObject.type); // will print 'cat'</pre>
<p>Now, what is going to happen if we have a nested Object inside our Object, doing the same operations?</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">let originalObject = {name: 'Bunt', details: {type: 'dog', color: 'brown'}};

let copiedObject = Object.assign({}, originalObject);

copiedObject.details.type = 'cat'; 

console.log(originalObject.details.type); // will print 'cat' 
console.log(copiedObject.details.type); // will print 'cat'</pre>
<p>As you can see, when you change the value of a property in the nested Object of <code>copiedObject</code>, that change is perpetuated to the same property of the nested Object in <code>originalObject</code>. You&#8217;ll ask why that happened? Because JavaScript passes the primitive values (string, number, etc.) as <code>value-copy</code> and the compound values (Objects, Arrays, Functions) as <code>reference-copy</code> to the value of the primitives on that Object. This way, when we copied the Object containing the nested Object, we have created a <code>shallow copy</code> of that object, meaning that the primitives found at the first level of the Object have values that are copied, thus when we change them into the <code>copiedObject</code>, they are not going to change in the <code>originalObject</code> while all other property found in a nested object will have the value passed as a <code>reference</code> to the same property value into the nested Object of <code>originalObject</code>, thus when we change its value, we actually change the value of the reference, resulting on both nested Objects having the modified property value.</p>
<p>Now, what&#8217;s the solution to <code>deep copy</code> an Object or Array, meaning that we want to have a copy free of references at all nested levels so we can change the values inside without the fear of breaking the original?</p>
<p>The first solution you are going to find is to serialize the Object to a JSON, then parse back that JSON to a JavaScript Object, practically passing the Object thru a string that will clean up the Object tree from the references nested inside by recreating a new Object with copied properties and values from the original one.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">let copiedObject = JSON.parse(JSON.stringify(originalObject));</pre>
<p>But this is a controversial solution. Why? Because this is going to work fine as long as your Objects and the nested Objects only contains primitives, but if you have objects containing <code>functions</code>, this won&#8217;t work. Neither for <code>Date</code>.</p>
<p>So, what&#8217;s the working solution? We are going to use <a href="https://lodash.com/">lodash&#8217;s</a> <a href="https://lodash.com/docs/4.17.4#cloneDeep">cloneDeep</a> method to <code>deep copy</code> the Object. Lodash is an excellent JavaScript utility library for those not knowing it yet. The <code>cloneDeep</code> method will iterate all levels of the original Object and recursively copying all properties found.</p>
<p>The example I will give here is in Angular. Since we don&#8217;t want to carry all the bundle of <code>lodash</code> to the client, we are going only to import the cloneDeep method.</p>
<p>First, let&#8217;s install <code>lodash</code>into our project:</p>
<p><code>yarn add lodash</code> or <code>npm install lodash</code></p>
<p>Once installed, we are going to use in our component:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">import * as cloneDeep from 'lodash/cloneDeep';

...

clonedObject = cloneDeep(originalObject);</pre>
<p>Importing <code>cloneDeep</code> will add to your build 18kb, a fair price to pay in order to have a reliable <code>deep copy</code> solution for you Objects.</p>
<p>I added a demo app on Github <a href="https://github.com/bogdancar/demo-angular-object-deep-copy">here</a>.</p>
<p><em>Interested in Angular? Join our <a href="https://www.meetup.com/Angular-Montreal/">Angular Montreal Meetup</a>.</em></p>The post <a href="https://blog.bogdancarpean.com/deepcopy-of-javascript-objects-and-arrays-using-lodashs-clonedeep-method/">Deepcopy of JavaScript Objects and Arrays using lodash’s cloneDeep method</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://blog.bogdancarpean.com/deepcopy-of-javascript-objects-and-arrays-using-lodashs-clonedeep-method/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
