How To Remove A Specific item From An Array

Are you looking to remove a specific item from an array? If you know the index of the array then depending upon the programming language you are using you can remove it using the inbuilt method Like Splice in JavaScript.

In this post, I will let you know how you can remove a specific item from an array in JavaScript. Also, you will learn how to remove the first element from the array, how to remove the last element from the array, and how to remove items from the array if you do not know the index.

How to Remove a Specific Item from Array in JavaScript

In JavaScript, we have an inbuilt method named splice(). This method can be used to remove an element from the array but you should be aware or know the index number that you want to remove in the first place.

The splice method in JavaScript takes an index as an input with a number of items required to be removed and it returns the new array that has an element without the one you chose to remove. Let us see that in below example code.

//Initializing the Array with Some Items
const givenArray = [10,11,12,13,14,15]

//Printing Array before Item being removed.
console.log("This is the Array Before Removal of Item.")
console.log(givenArray);

//Using indexOf Method to get index of the given item.
const index = givenArray.indexOf(12);

//Ensuring that Index is returned else do not perform
//Splice operation.
if (index > -1) {
  givenArray.splice(index, 1);
}

//Printing the Elements of Array after removal of Item
console.log("\nThis is the Array After Removal of Item.")
console.log(givenArray); 

Output:

This is the Array Before Removal of Item.
[ 10, 11, 12, 13, 14, 15 ]

This is the Array After Removal of Item.
[ 10, 11, 13, 14, 15 ]
How To Remove A Specific item From An Array

As you can see in the above code, I have removed item 12 from the array. I have used the indexOf method of Javascript to get the index number of item 12 and then remove that index from the array using the splice method.

How to Remove Front Item from Array in Javascript

If you want to just remove the first element from the array in JavaScript then you can use the shift method present in the JavaScript that moves the array from one item.

JavaScript’s shift() method is similar to the pop() method, except that it removes the first member of a JavaScript array, rather than the last. The remaining elements are pushed down when a component is eliminated.

This method removes items in place, does not remove duplicates, and does not use index or value as input.

Let us see in the below example code and how we can utilize the shift method to remove the first element in the array.

//Initializing the Array with Some Items
const givenArray = [10,11,12,13,14,15]

//Printing Array before Item being removed.
console.log("This is the Array Before Removal of Itemn.")
console.log(givenArray);

//Removing First Element of the Array using Shift
givenArray.shift();


//Printing the Elements of Array after removal of Item
console.log("\nThis is the Array After Removal of Item.")
console.log(givenArray); 

Output:

This is the Array Before Removal of Itemn.
[ 10, 11, 12, 13, 14, 15 ]

This is the Array After Removal of Item.
[ 11, 12, 13, 14, 15 ]

How To Remove Last Item of Array in JavaScript

This is really simple, as you might be already knowing there is a method called pop(). This pop method removes the last element of the array.

This method does remove items in place, does not remove duplicates, and does not use value or index as input. Also, this method completely removes the last element of the array and array length is altered to the new value.

Let us see in the below example code to understand how you can use it.

//Initializing the Array with Some Items
const givenArray = [10,11,12,13,14,15]

//Printing Array before Item being removed.
console.log("This is the Array Before Removal of Itemn.")
console.log(givenArray);

//Removing First Element of the Array using pop
givenArray.pop();


//Printing the Elements of Array after removal of Item
console.log("\nThis is the Array After Removal of Item.")
console.log(givenArray); 

Output:

This is the Array Before Removal of Itemn.
[ 10, 11, 12, 13, 14, 15 ]

This is the Array After Removal of Item.
[ 10, 11, 12, 13, 14 ]

Removing Element of the Array Using Delete Operator in JavaScript

The length attribute is unaffected by using the delete operator. The indexes of succeeding elements are unaffected. The deleted item is not removed from the array but rather becomes undefined, which is a sophisticated way of expressing that the array becomes sparse.

In simple words, the Array size remains the same and only the index that you wanted to delete is set to Undefined.

Let see that using the below example code on using of Delete Operator in JavaScript.

//Initializing the Array with Some Items
const givenArray = [10,11,12,13,14,15]

//Printing Array before Item being removed.
console.log("This is the Array Before Removal of Itemn.")
console.log(givenArray);

//Removing Specific Element of the Array using delete operator
indexToRemove = givenArray.indexOf(13);

delete givenArray[indexToRemove];


//Printing the Elements of Array after removal of Item
console.log("\nThis is the Array After Removal of Item.")
console.log(givenArray); 

Output:

This is the Array Before Removal of Itemn.
[ 10, 11, 12, 13, 14, 15 ]

This is the Array After Removal of Item.
[ 10, 11, 12, <1 empty item>, 14, 15 ]

As you can see in the above output, the array length is the same and the index of item 13 value is changed to an empty item.

Remove Element (Including Duplicates) From Array Using Filter Method

By including a filter function in the array, it is possible to remove a specific element from the array. Using this method if you want to remove all occurrences of specific values in the array then it is the best method to use.

This method does not remove elements in place, it creates a copy of the array and removes the duplicate using the value as input.

Let us see in the example code how you can remove duplicate values in an array in Javascript using the filter Method.

//Initializing the Array with Some Items
let givenArray = [10,3,11,12,3,13,14,3,3,15,3]

//Printing Array before Item being removed.
console.log("This is the Array Before Removal of Itemn.")
console.log(givenArray);

//Removing All the occurrence of number 3
//From the array using Filter Method
const valToRemove = 3;
givenArray = givenArray.filter(item => item!=valToRemove);


//Printing the Elements of Array after removal of Item
console.log("\nThis is the Array After Removal of Item.")
console.log(givenArray); 

Output:

This is the Array Before Removal of Itemn.
[
 10,  3, 11, 12,  3,
 13, 14,  3,  3, 15,
 3
]

This is the Array After Removal of Item.
[ 10, 11, 12, 13, 14, 15 ]

Wrap Up

I hope I was able to resolve your issues related to removing a specific item from an array. I tried to cover all the aspects that you can encounter in your daily coding life. Let me know if I am missing any better method in the comment section and I will be happy to add it.

Then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Convert a Char to String in C++ Quickly
  2. How to Remove Last Element From List In Python
  3. Equivalent to “source” in OpenBSD? Python Linux/Unix
  4. Trapping Rain Water Problem Python JAVA C++ Solution
  5. How to Write Enhanced For Loop in Java With Examples

Leave a Reply

Your email address will not be published. Required fields are marked *