How To Remove A Specific item From An Array

Do you want to delete a specific item from an array? If you know the index of the array, you can remove it using the inbuilt method in your programming language, such as Splice in JavaScript.

In this post, I’ll show you how to remove a specific item from an array using JavaScript. You will also learn how to remove the first and last elements of an array, as well as how to remove items from an array if you do not know the index.

How to Remove a Specific Item from Array in JavaScript

In JavaScript, we have a method called splice (). This method can be used to remove an element from an array, but you must first be aware of or know the index number that you want to remove.

In JavaScript, the splice method takes an index as an input and a number of items to be removed, and it returns a new array with an element missing from the one you chose to remove.

Let’s look at an example code below.

//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 from the code above, I removed item 12 from the array. I used Javascript’s indexOf method to get the index number of item 12 and then used the splice method to remove that index from the array.

How to Remove Front Item from Array in Javascript

If you only want to remove the first element from an array in JavaScript, you can use the shift method, which moves the array from one item to the next.

The shift() method in JavaScript is similar to the pop() method, except that it removes the first member of a JavaScript array rather than the last. When a component is removed, the remaining elements are pushed lower.

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

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

//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 extremely simple, as you may already be aware of the pop method (). This pop method removes the array’s last element.

This method removes items that are already in place, does not remove duplicates, and does not take a value or an index as input.

Additionally, this method removes the last element of the array and changes the array length to the new value.

Let’s look at the example code below to see 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 delete operator has no effect on the length attribute. The indexes of subsequent elements remain unaffected.

The deleted item is not removed from the array; rather, it becomes undefined, which is a more sophisticated way of saying that the array has become sparse.

Simply put, the Array size remains unchanged, and only the index you wanted to delete is set to Undefine.

Let’s look at how to use the Delete Operator in JavaScript using the example code below.

//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 ]

The array length remains the same in the above output, and the index of item 13 value is changed to an empty item.

Remove Element (Including Duplicates) From Array Using Filter Method

It is possible to remove a specific element from an array by including a filter function in the array. This method is the best to use if you want to remove all occurrences of specific values in the array.

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

Let’s look at an example of how to use the filter Method in Javascript to remove duplicate values from an array.

//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 help you resolve your issue with removing a specific item from an array.

I attempted to cover every aspect that you might encounter in your daily coding life. Please let me know if you find a better method in the comments section, and I will gladly 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 Comment