<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem:  Vihan Pamudya</title>
    <description>The latest articles on Forem by  Vihan Pamudya (@vihanpamudya).</description>
    <link>https://forem.com/vihanpamudya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F891238%2F80613457-7965-4e91-acce-bc2936c4c022.jpeg</url>
      <title>Forem:  Vihan Pamudya</title>
      <link>https://forem.com/vihanpamudya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vihanpamudya"/>
    <language>en</language>
    <item>
      <title>How to remove a specific item from an array?</title>
      <dc:creator> Vihan Pamudya</dc:creator>
      <pubDate>Wed, 13 Jul 2022 07:56:17 +0000</pubDate>
      <link>https://forem.com/vihanpamudya/how-can-i-remove-a-specific-item-from-an-array-4ijd</link>
      <guid>https://forem.com/vihanpamudya/how-can-i-remove-a-specific-item-from-an-array-4ijd</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Definition:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript arrays allow you to group values and iterate over them. You can add and remove array elements in different ways. Unfortunately, there is not a simple Array.remove method. There are &lt;strong&gt;9 ways&lt;/strong&gt; in total (use any of these which suits you). Instead of a delete method, the JavaScript array has a variety of ways you can clean array values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are different methods and techniques you can use to remove elements from JavaScript arrays:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;you can use it to remove elements from JavaScript arrays in these ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pop - Removes from the End of an Array&lt;/li&gt;
&lt;li&gt;shift - Removes from the beginning of an Array&lt;/li&gt;
&lt;li&gt;splice - removes from a specific Array index&lt;/li&gt;
&lt;li&gt;filter - allows you to programmatically remove elements from an Array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Method 1&lt;/strong&gt;: Removing Elements from the Beginning of a JavaScript Array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var ar = ['zero', 'one', 'two', 'three'];

ar.shift(); // returns "zero"

console.log( ar ); // ["one", "two", "three"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Method 2&lt;/strong&gt;: Removing Elements from the End of a JavaScript Array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var ar = [1, 2, 3, 4, 5, 6];

ar.length = 4; // set length to remove elements
console.log( ar ); // [1, 2, 3, 4]


var ar = [1, 2, 3, 4, 5, 6];

ar.pop(); // returns 6
console.log( ar ); // [1, 2, 3, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Method 3&lt;/strong&gt;: Using Splice to Remove Array Elements in JavaScript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];

var removed = arr.splice(2,2);
console.log(arr);


var list = ["bar", "baz", "foo", "qux"];

list.splice(0, 2);
console.log(list);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Method 4&lt;/strong&gt;: Removing Array Items By Value Using Splice&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr = [1, 2, 6, 3, 2, 6, 7, 8, 9, 10];

for (var i = 0; i &amp;lt; arr.length; i++) {

  if (arr[i] === 6) {

    var removed = arr.splice(i, 1);
    i--;
  }

}

console.log(arr); // 6 is removed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Method 5&lt;/strong&gt;: Using the Array filter Method to Remove Items By Value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

var filtered = array.filter(function(value, index, arr) {
  return value != 6 ;
});

console.log(filtered); // 6 is removed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Method 6&lt;/strong&gt;: The Lodash Array Remove Method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

var removeElement = _.remove(array, function(n) {
  return n === 6;
});

console.log(array); // 6 is removed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Method 7&lt;/strong&gt;: Making a Remove Method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function arrayRemove(arr, value) {

  return arr.filter(function(element) {
    return element != value;
  });
}

console.log(arrayRemove(array, 6)); // 6 is removed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Method 8&lt;/strong&gt;: Explicitly Remove Array Elements Using the Delete Operator&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var ar = [1, 2, 3, 4, 5, 6];

delete ar[4]; // delete element with index 4

console.log( ar ); // [1, 2, 3, 4, undefined, 6]

alert( ar ); // 1,2,3,4,,6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Method 9&lt;/strong&gt;: Clear or Reset a JavaScript Array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var ar = [1, 2, 3, 4, 5, 6];
//do stuffar = [];
//a new, empty array!

var arr1 = [1, 2, 3, 4, 5, 6];
var arr2 = arr1; 
// Reference arr1 by another variable arr1 = [];

console.log(arr2); 
// Output [1, 2, 3, 4, 5, 6]

var arr1 = [1, 2, 3, 4, 5, 6];
var arr2 = arr1; 
// Reference arr1 by another variable arr1 = [];

console.log(arr2); 
// Output [1, 2, 3, 4, 5, 6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Summary:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;It is important to remove JavaScript Array items to manage your data. There is no single 'remove' method, but there are various methods and techniques you can use to clean up unwanted array items.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
    </item>
  </channel>
</rss>
