{"id":6691,"date":"2020-04-14T14:59:36","date_gmt":"2020-04-14T14:59:36","guid":{"rendered":"https:\/\/cheesecakelabs.com\/blog\/?p=6691\/"},"modified":"2022-07-01T17:09:14","modified_gmt":"2022-07-01T17:09:14","slug":"avoid-array-mutation","status":"publish","type":"post","link":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/","title":{"rendered":"How to Avoid Array Mutation"},"content":{"rendered":"<p style=\"text-align: center;\"><em>Originally posted on <a href=\"https:\/\/dev.to\/helderburato\/how-to-avoid-array-mutation-4bpa\">dev.to<\/a><\/em><\/p>\n<p><span style=\"font-weight: 400;\">In this article, I&#8217;ll focus on showing how to add, edit and remove items in an array causing mutation and non-mutation ways.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">One thing we need to keep in mind when writing code avoiding mutation is to return a new reference to the data after the update.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">It&#8217;s a common approach when working with functional programming and if you want to understand some concepts of functional programming I recommend you read this <\/span><a href=\"https:\/\/dev.to\/helderburato\/understanding-concepts-of-functional-programming-with-javascript-2g1d\"><span style=\"font-weight: 400;\">article<\/span><\/a><span style=\"font-weight: 400;\"> I wrote some time ago.<\/span><\/p>\n<p><!--more--><\/p>\n<h2><span style=\"font-weight: 400;\">Why Avoid Mutation<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">When you work with immutable data you can have some positive impacts like the following:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8211; Tracking data without mutation is much better;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8211; Immutable states help you implement unidirectional data flow that helps you handle data;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">I really recommend you read this <\/span><a href=\"https:\/\/alistapart.com\/article\/why-mutation-can-be-scary\/\"><span style=\"font-weight: 400;\">article<\/span><\/a><span style=\"font-weight: 400;\"> to go deeper into why avoid mutation.<\/span><\/p>\n<h2>Causing Mutation<\/h2>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-6695\" src=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/mutation.jpg\" alt=\"\" width=\"2108\" height=\"962\" srcset=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/mutation.jpg 2108w, https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/mutation-768x350.jpg 768w\" sizes=\"(max-width: 2108px) 100vw, 2108px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">The following steps will cause mutation into the array when adding, removing and editing elements from `family`.<\/span><\/p>\n<p>To show an example of mutating, we&#8217;ll use the following array:<\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">const heroesMutate = ['Spider-man', 'Thor', 'Hulk', 'Iron Man'];\nconsole.log(heroesMutate); \/\/ =&gt; [\"Spider-man\", \"Thor\", \"Hulk\", \"Iron Man\"]\n<\/code><\/pre>\n<h3>Including in Array<\/h3>\n<p>Methods that will be used:<\/p>\n<ul>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-PT\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/push\"><span style=\"font-weight: 400;\">Array.prototype.push()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-PT\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/unshift\"><span style=\"font-weight: 400;\">Array.prototype.unshift()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-PT\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/splice\"><span style=\"font-weight: 400;\">Array.prototype.splice()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">See the following use-case examples for these methods:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nheroesMutate.push('Captain Marvel');\nconsole.log(heroesMutate); \/\/ =&gt; [\"Spider-man\", \"Thor\", \"Hulk\", \"Iron Man\", \"Captain Marvel\"]\n\nheroesMutate.unshift('Deadpool');\nconsole.log(heroesMutate); \/\/ =&gt; [\"Deadpool\", \"Spider-man\", \"Thor\", \"Hulk\", \"Iron Man\", \"Captain Marvel\"]\n\nheroesMutate.splice(2, 0, 'Black Panther');\nconsole.log(heroesMutate); \/\/ =&gt; [\"Deadpool\", \"Spider-man\", \"Black Panther\", \"Thor\", \"Hulk\", \"Iron Man\", \"Captain Marvel\"]\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Editing the Array<\/h3>\n<p><span style=\"font-weight: 400;\">The following case will find index for the element we want to edit and set value to the index found:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nconst indexDeadpool = heroesMutate.indexOf('Deadpool');\nheroesMutate[indexDeadpool] = 'Wolverine';\n\nconsole.log(heroesMutate); \/\/ =&gt; [\"Wolverine\", \"Spider-man\", \"Black Panther\", \"Thor\", \"Hulk\", \"Iron Man\", \"Captain Marvel\"]\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Removing in the Array<\/h3>\n<p><span style=\"font-weight: 400;\">Methods that will be used:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/pop\"><span style=\"font-weight: 400;\">Array.prototype.pop()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/shift\"><span style=\"font-weight: 400;\">Array.prototype.shift()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-PT\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/splice\"><span style=\"font-weight: 400;\">Array.prototype.splice()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">See the following use-case examples for these methods:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nheroesMutate.pop();\nconsole.log(heroesMutate); \/\/ =&gt; [\"Wolverine\", \"Spider-man\", \"Black Panther\", \"Thor\", \"Hulk\", \"Iron Man\"]\n\nheroesMutate.shift();\nconsole.log(heroesMutate); \/\/ =&gt; [\"Spider-man\", \"Black Panther\", \"Thor\", \"Hulk\", \"Iron Man\"]\n\nheroesMutate.splice(1, 1);\nconsole.log(heroesMutate); \/\/ =&gt; [\"Spider-man\", \"Thor\", \"Hulk\", \"Iron Man\"]\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>Avoiding Mutation<\/h2>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-6694\" src=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/without-mutation.jpg\" alt=\"\" width=\"2108\" height=\"962\" srcset=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/without-mutation.jpg 2108w, https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/without-mutation-768x350.jpg 768w\" sizes=\"(max-width: 2108px) 100vw, 2108px\" \/><\/p>\n<p>In this topic, we&#8217;ll add, remove and edit, avoiding mutations.<\/p>\n<p><span style=\"font-weight: 400;\">Methods that will be used:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/slice\"><span style=\"font-weight: 400;\">Array.prototype.slice()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/concat\"><span style=\"font-weight: 400;\">Array.prototype.concat()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-BR\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/map\"><span style=\"font-weight: 400;\">Array.prototype.map()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-BR\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/filtro\"><span style=\"font-weight: 400;\">Array.prototype.filter()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Spread_syntax\"><span style=\"font-weight: 400;\">Spread syntax<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">See the following use-cases:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nconst villains = ['Loki', 'Thanos', 'Venom', 'Abomination'];\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Including in the Array<\/h3>\n<p><span style=\"font-weight: 400;\">Add to the end of array:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nconst newVillains = villains.concat('Juggernaut');\nconst newVillains2 = [...newVillains, 'Magneto'];\nconst newVillains3 = ['Red Skull', ...newVillains2];\n\nconsole.log(villains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Venom\", \"Abomination\"]\nconsole.log(newVillains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Venom\", \"Abomination\", \"Juggernaut\"]\nconsole.log(newVillains2); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Venom\", \"Abomination\", \"Juggernaut\", \"Magneto\"]\nconsole.log(newVillains3); \/\/ =&gt; [\"Red Skull\", \"Loki\", \"Thanos\", \"Venom\", \"Abomination\", \"Juggernaut\", \"Magneto\"]\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">In the following example we&#8217;ll add <\/span><b>Ultron<\/b><span style=\"font-weight: 400;\">&nbsp; after <\/span><b>Thanos<\/b><span style=\"font-weight: 400;\"> in the array:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nconst newVillains = [...villains.slice(0, 2), 'Ultron', ...villains.slice(2, villains.length)];\n\nconsole.log(villains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Venom\", \"Abomination\"]\nconsole.log(newVillains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Ultron\", \"Venom\", \"Abomination\"]\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Editing the Array<\/h3>\n<p><span style=\"font-weight: 400;\">In the following example we&#8217;ll edit `Venom` to `Galactus`:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nconst indexVenom = villains.indexOf('Venom');\nconst newVillains = [...villains.slice(0, indexVenom), 'Galactus', ...villains.slice(indexVenom+1)];\nconst newVillains2 = newVillains.map(v =&gt; v === 'Abomination' ? 'Ultron' : v);\n\nconsole.log(villains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Venom\", \"Abomination\"]\nconsole.log(newVillains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Galactus\", \"Abomination\"]\nconsole.log(newVillains2); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Galactus\", \"Ultron\"]\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Removing in the Array<\/h3>\n<p><span style=\"font-weight: 400;\">In the following example we&#8217;ll remove <\/span><b>Thanos<\/b><span style=\"font-weight: 400;\"> from the array:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nconst indexThanos = villains.indexOf('Thanos');\nconst newVillains = [...villains.slice(0, indexHelder), ...villains.slice(indexHelder+1)];\nconst newVillains2 = newVillains.filter(v =&gt; v !== 'Thanos');\n\nconsole.log(villains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Venom\", \"Abomination\"]\nconsole.log(newVillains); \/\/ =&gt; [\"Loki\", \"Venom\", \"Abomination\"]\nconsole.log(newVillains2); \/\/ =&gt; [\"Loki\", \"Abomination\"]\n<\/code><\/pre>\n<p>See that in all the examples that we developed above, a new instance of the array was created, thus avoiding the mutation of the initially defined arrays.<\/p>\n<p>&nbsp;<\/p>\n<h2>Wrapping Up<\/h2>\n<p>Avoiding mutations is a safe and one-way path.<\/p>\n<p><span style=\"font-weight: 400;\">When you realize that you&#8217;re writing code observing this type of detail, believe me, you will be writing better, secure code and avoiding possible bugs due to mutation.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Feel free to share your feedback and experience in the comments.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Enjoy programming! \u2728<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">References<\/span><\/h2>\n<ul>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-PT\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\"><span style=\"font-weight: 400;\">Array &#8211; JavaScript | MDN<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/www.marvel.com\/teams-and-groups\">Marvel Teams, Groups, Squads, &amp; Alliances<\/a>;<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Originally posted on dev.to In this article, I&#8217;ll focus on showing how to add, edit and remove items in an array causing mutation and non-mutation ways. One thing we need to keep in mind when writing code avoiding mutation is to return a new reference to the data after the update. It&#8217;s a common approach [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":6693,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[432],"tags":[303,400],"class_list":["post-6691","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-engineering","tag-tag-frontend","tag-tag-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Avoid Array Mutation<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Avoid Array Mutation\" \/>\n<meta property=\"og:description\" content=\"Originally posted on dev.to In this article, I&#8217;ll focus on showing how to add, edit and remove items in an array causing mutation and non-mutation ways. One thing we need to keep in mind when writing code avoiding mutation is to return a new reference to the data after the update. It&#8217;s a common approach [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\" \/>\n<meta property=\"og:site_name\" content=\"Cheesecake Labs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cheesecakelabs\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-14T14:59:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-01T17:09:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2000\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Cheesecake Labs\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@cheesecakelabs\" \/>\n<meta name=\"twitter:site\" content=\"@cheesecakelabs\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\"},\"author\":{\"name\":\"Helder Burato Berto\"},\"headline\":\"How to Avoid Array Mutation\",\"datePublished\":\"2020-04-14T14:59:36+00:00\",\"dateModified\":\"2022-07-01T17:09:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\"},\"wordCount\":435,\"image\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png\",\"keywords\":[\"Front-end\",\"JavaScript\"],\"articleSection\":[\"Engineering\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\",\"url\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\",\"name\":\"How to Avoid Array Mutation\",\"isPartOf\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png\",\"datePublished\":\"2020-04-14T14:59:36+00:00\",\"dateModified\":\"2022-07-01T17:09:14+00:00\",\"author\":{\"@type\":\"person\",\"name\":\"Helder Burato Berto\"},\"breadcrumb\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage\",\"url\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png\",\"contentUrl\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png\",\"width\":2000,\"height\":720,\"caption\":\"Round purple puzzle piece becoming a square purple puzzle piece\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cheesecakelabs.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Avoid Array Mutation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/#website\",\"url\":\"https:\/\/cheesecakelabs.com\/blog\/\",\"name\":\"Cheesecake Labs\",\"description\":\"Nearshore outsourcing company for Web and Mobile design and engineering services, and staff augmentation for startups and enterprises..\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cheesecakelabs.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"name\":\"Helder Burato Berto\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/#\/schema\/person\/image\/\",\"url\":false,\"contentUrl\":false,\"caption\":\"Helder Burato Berto\"},\"description\":\"10 years of experience in Marketing and Sales in the Technology sector. My main purpose is help, support and structure efficient operations and also develop independent and multidisciplinary teams.\",\"url\":\"https:\/\/cheesecakelabs.com\/blog\/autor\/helder\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Avoid Array Mutation","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/","og_locale":"en_US","og_type":"article","og_title":"How to Avoid Array Mutation","og_description":"Originally posted on dev.to In this article, I&#8217;ll focus on showing how to add, edit and remove items in an array causing mutation and non-mutation ways. One thing we need to keep in mind when writing code avoiding mutation is to return a new reference to the data after the update. It&#8217;s a common approach [&hellip;]","og_url":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/","og_site_name":"Cheesecake Labs","article_publisher":"https:\/\/www.facebook.com\/cheesecakelabs","article_published_time":"2020-04-14T14:59:36+00:00","article_modified_time":"2022-07-01T17:09:14+00:00","og_image":[{"width":2000,"height":720,"url":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png","type":"image\/png"}],"author":"Cheesecake Labs","twitter_card":"summary_large_image","twitter_creator":"@cheesecakelabs","twitter_site":"@cheesecakelabs","twitter_misc":{"Written by":null,"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#article","isPartOf":{"@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/"},"author":{"name":"Helder Burato Berto"},"headline":"How to Avoid Array Mutation","datePublished":"2020-04-14T14:59:36+00:00","dateModified":"2022-07-01T17:09:14+00:00","mainEntityOfPage":{"@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/"},"wordCount":435,"image":{"@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage"},"thumbnailUrl":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png","keywords":["Front-end","JavaScript"],"articleSection":["Engineering"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/","url":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/","name":"How to Avoid Array Mutation","isPartOf":{"@id":"https:\/\/cheesecakelabs.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage"},"image":{"@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage"},"thumbnailUrl":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png","datePublished":"2020-04-14T14:59:36+00:00","dateModified":"2022-07-01T17:09:14+00:00","author":{"@type":"person","name":"Helder Burato Berto"},"breadcrumb":{"@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage","url":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png","contentUrl":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png","width":2000,"height":720,"caption":"Round purple puzzle piece becoming a square purple puzzle piece"},{"@type":"BreadcrumbList","@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cheesecakelabs.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Avoid Array Mutation"}]},{"@type":"WebSite","@id":"https:\/\/cheesecakelabs.com\/blog\/#website","url":"https:\/\/cheesecakelabs.com\/blog\/","name":"Cheesecake Labs","description":"Nearshore outsourcing company for Web and Mobile design and engineering services, and staff augmentation for startups and enterprises..","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cheesecakelabs.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","name":"Helder Burato Berto","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cheesecakelabs.com\/blog\/#\/schema\/person\/image\/","url":false,"contentUrl":false,"caption":"Helder Burato Berto"},"description":"10 years of experience in Marketing and Sales in the Technology sector. My main purpose is help, support and structure efficient operations and also develop independent and multidisciplinary teams.","url":"https:\/\/cheesecakelabs.com\/blog\/autor\/helder\/"}]}},"_links":{"self":[{"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/posts\/6691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/comments?post=6691"}],"version-history":[{"count":1,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/posts\/6691\/revisions"}],"predecessor-version":[{"id":10174,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/posts\/6691\/revisions\/10174"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/media\/6693"}],"wp:attachment":[{"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/media?parent=6691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/categories?post=6691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/tags?post=6691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}