{"id":236,"date":"2014-06-01T17:06:15","date_gmt":"2014-06-01T17:06:15","guid":{"rendered":"http:\/\/localhost\/__sites\/3d_blog\/?p=236"},"modified":"2022-05-24T20:18:59","modified_gmt":"2022-05-24T20:18:59","slug":"shrink-an-array","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=236","title":{"rendered":"shrink an array"},"content":{"rendered":"<p>When you drop an element from an array by <code data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">delete item[2];<\/code><br \/>\nThe second item will be there with <code data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">undefined<\/code> value. Let&#8217;s delete more than one item from the array and there will be many holes in the line.<br \/>\nHere is a simple shrink function to solve this problem. This is a simple Array extension.<\/p>\n<pre>\r\nvar shrink = function() {\r\n  var ln = this.length;\r\n  for(var i=0; i < ln; i++){\r\n    if(this[i] != undefined){ this.push(this[i]); }\r\n  }\r\n  this.splice(0, ln);\r\n  return this;\r\n};\r\nTypeExtend(Array, 'shrink', shrink);\r\n<\/pre>\n<p>I used a little bit strange but quick solution.<br \/>\n1. I need the original length of the Array<br \/>\n2. Push every defined (with value) item to the end of the Array<br \/>\n3. cut the original items from the start of the Array<br \/>\n4. the result is a clean Array<\/p>\n<p>try this<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\nvar f = [1,2,3,4,5];\r\ndelete f[2];\r\ndelete f[4];\r\nconsole.log(f);\r\n\/\/ [1, 2, undefined \u00d7 1, 4, undefined \u00d7 1]\r\nf.shrink();\r\nconsole.log(f);\r\n\/\/ [1, 2, 4] \r\n<\/pre>\n<p>It's working nicely.<\/p>\n<blockquote><p>see also: <a href=\"?p=232\" title=\"extender \u2013 Array\">extender \u2013 Array<\/a>, <a href=\"?p=23\" title=\"javascript extender\">javascript extender (TypeExtend)<\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>When you drop an element from an array by The second item will be there with value. Let&#8217;s delete more than one item from the array and there will be many holes in the line. Here is a simple shrink function to solve this problem. This is a simple Array extension. var shrink = function() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-236","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=236"}],"version-history":[{"count":28,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/236\/revisions"}],"predecessor-version":[{"id":1183,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/236\/revisions\/1183"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}