{"id":9618,"date":"2024-02-15T04:52:36","date_gmt":"2024-02-15T04:52:36","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=9618"},"modified":"2024-02-15T06:07:45","modified_gmt":"2024-02-15T06:07:45","slug":"foreach-loop-over-an-array-in-javascript","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/javascript\/foreach-loop-over-an-array-in-javascript","title":{"rendered":"Loop (for each) Over an Array in JavaScript"},"content":{"rendered":"<p>In JavaScript, you can remove a specific item from an array using various methods. Here are some common ways to do so:<\/p>\n<h3>Using filter() method<\/h3>\n<p>The filter() method creates a new array with elements that pass the provided test. It doesn&#8217;t mutate the original array.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let array = [1, 2, 3, 4, 5];\r\nconst itemToRemove = 3; \/\/ Item to remove\r\narray = array.filter(item =&gt; item !== itemToRemove);\r\n\r\nconsole.log(array); \/\/ Output: [1, 2, 4, 5]<\/pre>\n<h3>Using splice() method<\/h3>\n<p>The splice() method changes the contents of an array by removing or replacing existing elements. It modifies the array in place.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\nlet array = [1, 2, 3, 4, 5];\r\nconst itemToRemove = 3; \/\/ Item to remove\r\nconst indexToRemove = array.indexOf(itemToRemove);\/\/ Index of the item to remove\r\n\r\narray.splice(indexToRemove, 1); \/\/ Removes one element at the specified index\r\n\r\nconsole.log(array); \/\/ Output: [1, 2, 4, 5]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript, you can remove a specific item from an array using various methods. Here are some common ways to do so: Using filter() method The filter() method creates a new array with elements that pass the provided test. It doesn&#8217;t mutate the original array. let array = [1, 2, 3, 4, 5]; const itemToRemove [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9619,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[22],"tags":[],"class_list":["post-9618","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9618"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=9618"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9618\/revisions"}],"predecessor-version":[{"id":9621,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9618\/revisions\/9621"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/9619"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=9618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=9618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=9618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}