E-Learning

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.

var arr = [1, 2, 3, 4, 5];
console.log ( "Original Array .." + arr);
arr.pop ();
console.log ( "After pop () ...." + arr);
arr.push (8);
console.log ( "After push () ..." + arr);
arr.shift ();
console.log ( "After shift () .." + arr);
arr.unshift (7);
console.log ( "After unshift ()" + arr);

The above code will result in the following sequence:

Original Array .. 1, 2, 3, 4, 5
After pop () .. 1, 2, 3, 4
After push () .. 1, 2, 3, 4, 8
After shift () ..: 2, 3, 4, 8
After unshift () ..: 7, 2, 3, 4, 8

 

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.

(Array) arr_original.splice (index, quantity, elem1, ..., elemX);

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.

arr= [1, 2, 3, 4, 5];
arr.splice (2, 2);
// [1, 2, 5]

arr= [1, 2, 3, 4, 5];
arr.splice (3, 0, 6);
// [1, 2, 3, 6, 4, 5]

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

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:

LearnToProgram

JDdesign

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

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

3 weeks ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

1 month ago

Highest Stable Coin Yields – (W12 – 2024)

It’s another week and like always we have to explore the top yield platforms for…

1 month ago

Binance Auto Invest – the Best Innovation in Crypto since Sliced Bread

At a time where we’re constantly seeking tools and strategies to simplify our crypto investments,…

2 months ago

Highest Stable Coin Yields – March 2024

As we kick off another week, it's time to explore the top yield platforms for…

2 months ago

Education Ecosystem Featured on Business Insider

We're excited to share that Education Ecosystem was recently featured in an article on Business…

2 months ago