{"id":1776,"date":"2017-04-12T12:52:12","date_gmt":"2017-04-12T10:52:12","guid":{"rendered":"https:\/\/blog.kodono.info\/wordpress\/?p=1776"},"modified":"2017-05-26T12:23:24","modified_gmt":"2017-05-26T10:23:24","slug":"deep-clone-an-object-in-javascript","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2017\/04\/12\/deep-clone-an-object-in-javascript\/","title":{"rendered":"Deep clone an object in JavaScript"},"content":{"rendered":"<p>We can find many discussions and solutions about this issue.<\/p>\n<p>In my case the one that worked is <a href=\"http:\/\/stackoverflow.com\/a\/24248152\/1134119\">this code<\/a>.<\/p>\n<p>Below you&#8217;ll find a compressed version of the <code>extend()<\/code> function (<a href=\"https:\/\/stackoverflow.com\/questions\/122102\/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript\/24248152#comment75410509_24248152\">with a fix<\/a>):<\/p>\n<pre class=\"brush:javascript\">\r\nfunction extend(){var r,t,n,o,e=arguments[0]||{},f=1,i=arguments.length,u=!1,y=function(r){if(null===r||\"object\"!=typeof r||r.nodeType||null!==r&#038;&r===r.window)return!1;try{if(r.constructor&#038;&!this.hasOwnProperty.call(r.constructor.prototype,\"isPrototypeOf\"))return!1}catch(t){return!1}return!0};for(\"boolean\"==typeof e&#038;&(u=e,e=arguments[f]||{},f++),\"object\"!=typeof e&#038;&\"function\"!=typeof e&#038;&(e={}),!1;i>f;f+=1)if(null!==(r=arguments[f]))for(t in r)e!==r[t]&#038;&\"undefined\"==typeof e[t]&#038;&(u&#038;&r[t]&#038;&(y(r[t])||(n=Array.isArray(r[t])))?(n?(n=!1,o=e[t]&#038;&Array.isArray(e[t])?e[t]:[]):o=e[t]&#038;&y(e[t])?e[t]:{},e[t]=extend(u,o,r[t])):void 0!==r[t]&#038;&(e[t]=r[t]));return e}\r\n<\/pre>\n<p>And here an example:<\/p>\n<pre class=\"brush:javascript\">\r\nvar object_a = {\r\n    value_1: 'a',\r\n    value_2: true,\r\n    value_3: 354,\r\n    value_4: [\r\n      'x',\r\n      'y',\r\n      'z'\r\n    ],\r\n    value_5: {\r\n      I: 'I',\r\n      II: 'II',\r\n      III: 'III',\r\n      IV: {\r\n        a: 'A',\r\n        b: 'B'\r\n      },\r\n      V: [\r\n        'elm', {\r\n          5: 'five'\r\n        },\r\n        ['my_array']\r\n      ]\r\n    },\r\n    value_6: function() {\r\n      return \"pouet\"\r\n    },\r\n    value_7: new Date(2017, 0, 1)\r\n  };\r\nvar object_b = {};\r\n\/\/ clone object_a into object_b\r\nextend(true, object_b, object_a);\r\nobject_a.value_2 = false;\r\nobject_a.value_1 = \"b\";\r\nconsole.log(\"A => \", object_a);\r\nconsole.log(\"B => \", object_b);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We can find many discussions and solutions about this issue. In my case the one that worked is this code. Below you&#8217;ll find a compressed version of the extend() function (with a fix): function extend(){var r,t,n,o,e=arguments[0]||{},f=1,i=arguments.length,u=!1,y=function(r){if(null===r||&#8221;object&#8221;!=typeof r||r.nodeType||null!==r&#038;&#038;r===r.window)return!1;try{if(r.constructor&#038;&#038;!this.hasOwnProperty.call(r.constructor.prototype,&#8221;isPrototypeOf&#8221;))return!1}catch(t){return!1}return!0};for(&#8220;boolean&#8221;==typeof e&#038;&#038;(u=e,e=arguments[f]||{},f++),&#8221;object&#8221;!=typeof e&#038;&#038;&#8221;function&#8221;!=typeof e&#038;&#038;(e={}),!1;i>f;f+=1)if(null!==(r=arguments[f]))for(t in r)e!==r[t]&#038;&#038;&#8221;undefined&#8221;==typeof e[t]&#038;&#038;(u&#038;&#038;r[t]&#038;&#038;(y(r[t])||(n=Array.isArray(r[t])))?(n?(n=!1,o=e[t]&#038;&#038;Array.isArray(e[t])?e[t]:[]):o=e[t]&#038;&#038;y(e[t])?e[t]:{},e[t]=extend(u,o,r[t])):void 0!==r[t]&#038;&#038;(e[t]=r[t]));return e} And here an example: var object_a = { value_1: &#8216;a&#8217;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":"","footnotes":""},"categories":[170,13,33],"tags":[123,24,152,158],"class_list":["post-1776","post","type-post","status-publish","format-standard","hentry","category-english","category-niveau-intermediaire","category-programmation","tag-english","tag-javascript","tag-niveau-intermediaire","tag-programmation"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1776"}],"version-history":[{"count":5,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1776\/revisions"}],"predecessor-version":[{"id":1803,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1776\/revisions\/1803"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}