Learn JavaScript – How To Remove a Particular Element From an Array?

 
When programming in JavaScript, sometimes we want to manipulate items within an Array. In this article, we show how to remove or add elements in an array using native JavaScript methods.
 

JavaScript – Manipulating elements at the beginning or end of an Array

Add or remove items at the beginning or end of the array is simple. The functions pop () push (), shift () and unshift () can be used for this.

The first two functions manipulate final items in the array while shift () and unshift () manipulate the initial elements. Below is an example.


The above code will result in the following sequence:


 

JavaScript – Manipulating intermediate items from an Array

Working with items that are in the middle of an array is not so easy, but we have the splice () function.  Its syntax is simple and requires the following parameters: the original array, the initial position, the amount of items that will be affected, and if we are adding items, their value.


The examples below show how to remove two items from the array, from the second element and the addition of a new element in the 4th position.


Important: the function makes changes directly in the original array and returns an array of items that have been affected – added or removed.

ENJOYING THIS POST?

Get 5% discount on your monthly subscription purchase

Do you know other ways to do this operation in JavaScript?  Share your comments in the section below!

If you want to explore other questions, you can check our videos about JavaScript. Below are some examples:

You can also subscribe to some channels that broadcast in JavaScript, such as the following:

JavaScript array how to remove an element from an array programmingLearnToProgram

JavaScript array how to remove an element from an array programmingJDdesign

Another cool way to find out interesting things about JavaScript is to access our project page!

  • Edgar Burkhart

    Thanks for this clear and simple explanation. Solved my overall issue. Keep it up livecoding

Read previous post:
leonchen83-photo
Livecoding.tv直播者采访:“我希望写码写到退休”

在国内一家互联网公司任职软件工程师的Leon,在一个偶然的机会下认识了livecoding.tv ,并成为了平台上的直播者。他在接受我们的采访时,分享了他的编程经验和职业规划。他热爱编程,坦言会写代码写到退休。

Close