{"id":386,"date":"2012-11-23T10:22:46","date_gmt":"2012-11-23T10:22:46","guid":{"rendered":"http:\/\/codeatelier.wordpress.com\/?p=386"},"modified":"2016-09-10T14:34:39","modified_gmt":"2016-09-10T14:34:39","slug":"how-to-remove-element-from-javascript-array","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/how-to-remove-element-from-javascript-array\/","title":{"rendered":"How to remove element from javascript array"},"content":{"rendered":"<p>The below function takes array element which needs to be removed as parameter as below<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nary.remove(\u2018One\u2019);\r\nvar ary = &#x5B;\u2018One\u2019, \u2018Two\u2019, \u2018Three\u2019];\r\nArray.prototype.remove = function() {\r\n    var what, a = arguments, L = a.length, ax;\r\n    while (L &amp;&amp; this.length) {\r\n        what = a&#x5B;--L];\r\n        while ((ax = this.indexOf(what)) !== -1) {\r\n            this.splice(ax, 1);\r\n        }\r\n    }\r\n    return this;\r\n};\r\n<\/pre>\n<p>The above method works perfectly works for java script array with strings in it.<\/p>\n<p>For the below variable<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar csvEmpIds = \u20181,2,3,4,5\u2019;\r\narrEmpIds = csvEmpIds.split(\u2018,\u2019);\r\nary.remove(2);\r\n<\/pre>\n<p>The above will not work as you should pass 2 as a string and split function creates<br \/>\na array by splitting from one variable and putting as a strings in array.<\/p>\n<p>so csvEmpIds.split(&#8216;,&#8217;) will create array like below<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\narrEmpIds = &#x5B;'1','2','3','4','5'];\r\n<\/pre>\n<p>In this case to remove element from array which you got by splitting come delimited value you should<br \/>\ndo as below.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nary.remove('2');\r\n<\/pre>\n<p>To Remove array which is declared globally use the below function<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar ary = &#x5B;'three', 'seven', 'eleven'];\r\nremoveA(ary, 'seven');\r\n<\/pre>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction removeA(arr)\r\n{\r\n  var what, a = arguments, L = a.length, ax;\r\n  \r\n  while (L &gt; 1 &amp;&amp; arr.length) \r\n  {\r\n    what = a&#x5B;--L];\r\n    while ((ax= arr.indexOf(what)) !== -1) \r\n    {\r\n      arr.splice(ax, 1);\r\n    }\r\n   }\r\n\r\n   return arr;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The below function takes array element which needs to be removed as parameter as below ary.remove(\u2018One\u2019); var ary = &#x5B;\u2018One\u2019, \u2018Two\u2019, \u2018Three\u2019]; Array.prototype.remove = function() { var what, a = arguments, L = a.length, ax; while (L &amp;&amp; this.length) { what = a&#x5B;&#8211;L]; while ((ax = this.indexOf(what)) !== -1) { this.splice(ax, 1); } } return&hellip; <a href=\"https:\/\/codethataint.com\/blog\/how-to-remove-element-from-javascript-array\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[],"class_list":["post-386","post","type-post","status-publish","format-standard","hentry","category-arrays-javascript"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=386"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/386\/revisions"}],"predecessor-version":[{"id":1592,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/386\/revisions\/1592"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}