How Can I Remove a Specific Item from an Array? (2024)

How Can I Remove a Specific Item from an Array? (1)

James W.

The ProblemJump To Solution

If you want to remove an item from an array, you can use the pop() method to remove the last element or the shift() method to remove the first element.

However, if the item you want to remove is not the first or last element, these methods are not the tools for the job.

The Solution

There are a few methods you can use to remove specific elements from an array in JavaScript. If the pop() or shift() methods won’t work for your purposes, you can select a method depending on whether you’ll identify the item by its value or its index.

Removing the Last Element of an Array

The pop() method removes and returns the last element of an array.

Click to Copy

const myArray = [1, 2, 3, 4, 5];const x = myArray.pop();console.log(`myArray values: ${myArray}`);console.log(`variable x value: ${x}`);

Output:

Removing the First Element of an Array

The shift() method removes and returns the first element of an array.

Click to Copy

const myArray = [1, 2, 3, 4, 5];const x = myArray.shift();console.log(`myArray values: ${myArray}`);console.log(`variable x value: ${x}`);

Output:

Click to Copy

myArray values = 2,3,4,5variable x value: 1

Removing an Element by Index

If you are identifying the item to be removed by its index, you can use the delete operator. If you want to use the value of the item you are removing, use the splice() method.

The delete Operator

The delete operator deletes the object property at the specified index, but does not update the length of the array, and the value at that index of the array will be undefined.

Click to Copy

const myArray = [1, 2, 3, 4, 5];delete myArray[1];console.log(`myArray values: ${myArray}`);

Output:

Click to Copy

myArray values: 1,,3,4,5

The splice() Method

The splice() method takes two arguments, the index of the element you wish to remove and the index you wish to remove up to.

The splice() method creates a new array that stores all the values that were removed from the original array. The original array will no longer contain the values removed, and its length will be updated.

Click to Copy

const myArray = [1, 2, 3, 4, 5];const x = myArray.splice(1, 1);console.log(`myArray values: ${myArray}`);console.log(`variable x value: ${x}`);

Output:

Click to Copy

myArray values: 1,3,4,5variable x value: 2

Removing an Element by Value

If you are identifying the element to be removed by its value, you can delete the element from its index after identifying the index with the indexOf() method. If you want to use the value of the element you are removing, then use the filter() method, or a combination of the indexOf() and splice() methods.

Combining indexOf() and splice() Methods

Pass the value of the element you wish to remove from your array into the indexOf() method to return the index of the element that matches that value in the array. Then make use of the splice() method to remove the element at the returned index.

Click to Copy

const myArray = [1, 2, 3, 4, 5];const index = myArray.indexOf(2);const x = myArray.splice(index, 1);console.log(`myArray values: ${myArray}`);console.log(`variable x value: ${x}`);

Output:

Click to Copy

myArray values: 1,3,4,5variable x value: 2

The indexOf() method returns the first index where the value matches the parameter passed into it, and -1 if no value is found to match.

The filter() Method

The array filter() method takes in a function as a required argument. The function requires one parameter currentValue, which represents the element in the array the filter() method is currently on while looping through the array.

The function should perform a check that returns true if the currentValue is the value of the element you wish to remove, and false if not.

The filter() method then returns an array that contains the values from the array that match the currentValue.

The values the filter() method returns are not removed from the original array, but that functionality can be added to the function you pass into filter().

Click to Copy

const myArray = [1, 2, 3, 4, 5];function removeValue(value, index, arr) { // If the value at the current array index matches the specified value (2) if (value === 2) { // Removes the value from the original array arr.splice(index, 1); return true; } return false;}// Pass the removeValue function into the filter function to return the specified valueconst x = myArray.filter(removeValue);console.log(`myArray values: ${myArray}`);console.log(`variable x value: ${x}`);

Output:

Click to Copy

myArray values: 1,3,4,5variable x value: 2

Further Reading

If you’re looking to get a deeper understanding of how JavaScript application monitoring works, take a look at the following articles:

How Can I Remove a Specific Item from an Array? (2024)
Top Articles
How To Sell Dogecoin | Best Ways To Cash Out Your DOGE 2023
What Are the Six Requirements for Successful Value Chain Management? by Charles Green
Ron Martin Realty Cam
Garrison Blacksmith Bench
Pinellas County Jail Mugshots 2023
Maria Dolores Franziska Kolowrat Krakowská
From Algeria to Uzbekistan-These Are the Top Baby Names Around the World
Overnight Cleaner Jobs
Shorthand: The Write Way to Speed Up Communication
Marist Dining Hall Menu
Directions To Lubbock
Mlifeinsider Okta
Midway Antique Mall Consignor Access
Jcpenney At Home Associate Kiosk
Richmond Va Craigslist Com
Shemal Cartoon
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Wisconsin Women's Volleyball Team Leaked Pictures
Craigslist Apartments In Philly
Alexandria Van Starrenburg
Truth Of God Schedule 2023
U Arizona Phonebook
Candy Land Santa Ana
The EyeDoctors Optometrists, 1835 NW Topeka Blvd, Topeka, KS 66608, US - MapQuest
LCS Saturday: Both Phillies and Astros one game from World Series
Happy Homebodies Breakup
Synergy Grand Rapids Public Schools
Egusd Lunch Menu
10 Best Places to Go and Things to Know for a Trip to the Hickory M...
Radical Red Ability Pill
Great ATV Riding Tips for Beginners
Royalfh Obituaries Home
Taylored Services Hardeeville Sc
Sinai Sdn 2023
How often should you visit your Barber?
Club Keno Drawings
Baldur's Gate 3 Dislocated Shoulder
Compress PDF - quick, online, free
Magicseaweed Capitola
Hometown Pizza Sheridan Menu
T&Cs | Hollywood Bowl
Author's Purpose And Viewpoint In The Dark Game Part 3
8776725837
About Us
DL381 Delta Air Lines Estado de vuelo Hoy y Historial 2024 | Trip.com
303-615-0055
Clock Batteries Perhaps Crossword Clue
View From My Seat Madison Square Garden
Tyrone Unblocked Games Bitlife
O'reilly's Eastman Georgia
Law Students
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 6213

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.