{"id":734,"date":"2014-09-13T19:26:58","date_gmt":"2014-09-13T19:26:58","guid":{"rendered":"http:\/\/blog.silverterra.net\/?p=734"},"modified":"2015-02-07T01:27:38","modified_gmt":"2015-02-07T01:27:38","slug":"js-array-cheet-sheet","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=734","title":{"rendered":"js array Cheet Sheet"},"content":{"rendered":"<blockquote><p>Goal: Collect the array manipulation methods<\/p><\/blockquote>\n<p><b>1. add item to an array<\/b><\/p>\n<pre data-enlighter-language=\"html\" class=\"EnlighterJSRAW\">\r\n  unshift  -&gt;  array  &lt;-  push\r\n[\/html]\r\n&lt;strong&gt;unshift&lt;\/strong&gt;: The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.\r\n&lt;strong&gt;push&lt;\/strong&gt;: The push() method adds one or more elements to the end of an array and returns the new length of the array.\r\n\r\n&lt;b&gt;2. remove item from an array&lt;\/b&gt;\r\n\r\n[html]\r\n  shift    &lt;-  array  -&gt;  pop\r\n<\/pre>\n<p><strong>shift:<\/strong> The shift() method removes the first element from an array and returns that element. This method changes the length of the array.<br \/>\n<strong>pop:<\/strong> The pop() method removes the last element from an array and returns that element.<\/p>\n<p><b>3. insert item to an array<\/b><br \/>\nThere is no general solution to do this. Let's extend the array object with the insert method like this:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\nArray.prototype.insert = function(index) {\r\n  index = Math.min(index, this.length);\r\n  arguments.length &gt; 1\r\n    &amp;&amp; this.splice.apply(this, [index, 0].concat([].pop.call(arguments)))\r\n    &amp;&amp; this.insert.apply(this, arguments);\r\n  return this;\r\n};\r\n<\/pre>\n<p>and the calling is:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\n  [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;].insert(2, &quot;V&quot;, [&quot;X&quot;, &quot;Y&quot;, &quot;A&quot;], &quot;Z&quot;);\r\n  the result will be: [&quot;a&quot;, &quot;b&quot;, &quot;V&quot;, &quot;X&quot;, &quot;Y&quot;, &quot;A&quot;, &quot;Z&quot;, &quot;c&quot;]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Goal: Collect the array manipulation methods 1. add item to an array shift: The shift() method removes the first element from an array and returns that element. This method changes the length of the array. pop: The pop() method removes the last element from an array and returns that element. 3. insert item to an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,3],"tags":[],"class_list":["post-734","post","type-post","status-publish","format-standard","hentry","category-cheetsheet","category-javascript"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/734","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=734"}],"version-history":[{"count":1,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/734\/revisions"}],"predecessor-version":[{"id":735,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/734\/revisions\/735"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}