Remove Specific Item From an Array

Remove Specific Item From an Array

In this example, you will learn to write a JavaScript program that will remove a specific item from an array.

To understand this example, you should have the knowledge of the following JavaScript programming topics:

JavaScript Array push()

JavaScript Array splice()

JavaScript for loop


Example 1: Using For Loop



In the above program, an item is removed from an array using a for loop.


Here,


The for loop is used to loop through all the elements of an array.

While iterating through the elements of the array, if the item to remove does not match with the array element, that element is pushed to newArray.

The push() method adds the element to newArray.


Example 2: Using Array.splice()



In the above program, an array and the element to be removed is passed to the custom removeItemFromArray() function.


The indexOf() method returns the index of the given element.

If the element is not in the array, indexOf() returns -1.

The if condition checks if the element to remove is in the array.

The splice() method is used to remove the element from an array.


Post a Comment

Previous Post Next Post