How to Remove a Specific Item from an Array in JavaScript ?
Last Updated :
21 Oct, 2024
Given an Array, the task is remove specific item from an array in JavaScript. It means we have an array with N items, and remove a particular item from array.
Examples
Input: Arr = [ 10, 20, 30, 40, 50, 60 ]
removeItem = 40
Output: [ 10, 20, 30, 50, 60 ]
Using splice() Method
The Array splice() method is used to remove an item from the array by its index. It modifies the original array.
Syntax
Array.splice( index, remove_count, item_list );
Example: Remove specific item 30 from given array [10, 20, 30, 40, 50] using splice() method.
JavaScript
function removeItem(array, itemToRemove) {
const index = array.indexOf(itemToRemove);
if (index !== -1) {
array.splice(index, 1);
}
console.log("Updated Array: ", array);
}
// Driver Code
const arr = [ 10, 20, 30, 40, 50 ];
// Remove item 30 from Array
removeItem( arr, 30 );
OutputUpdated Array: [ 10, 20, 40, 50 ]
Using filter() Method
The Array filter() Method creates new array by excluding the item that you want to remove.
Syntax
array.filter( callback(element, index, arr), thisValue );
Example: Remove specific item 30 from array [10, 20, 30, 40, 50] using filter() method.
JavaScript
function removeItem(array, itemToRemove) {
return array.filter(
item => item !== itemToRemove);
}
// Driver code
const arr = [ 10, 20, 30, 40, 50 ];
// Remove item 30 from Array
console.log("Updated Array: ", removeItem(arr, 30));
OutputUpdated Array: [ 10, 20, 40, 50 ]
Using slice() and indexOf() Methods
The indexOf() method finds the index of item which need to remove. If the item is found then it creates a new array using slice() method to extract elements of before and after the found item index. At last, use spread operator combine the sliced array.
JavaScript
function removeItem(array, itemToRemove) {
let index = array.indexOf(itemToRemove);
let newArr = index !== -1 ?
[...array.slice(0, index), ...array.slice(index + 1)] : fruits;
return newArr;
}
// Driver code
let arr = [ 10, 20, 30, 40, 50 ];
// Remove item 30 from Array
console.log(removeItem(arr, 30));
Using indexOf(), slice(), and concat() Methods
The indexOf()
method used to find the index of item to be removed. If the item is found then it creates a new array and use slice() method
to extract elements of before and after the found item index. At last, use concat() method to combine the sliced array.
For Example, given an array [ 10, 20, 30, 40, 50 ], and we have to remove item 30 from array. The indexOf() method find the index of item 30, which is 2. The array is then split into two parts using the slice() method: [10, 20] (elements before index 2) and [40, 50] (elements after index 2). These two sub-arrays are combined using the concat() method, resulting in [10, 20, 40, 50], effectively removing 30 from the array.
Example: Removing the item 30 from array using indexOf(), slice() and concat() methods.
JavaScript
function removeItem(array, itemToRemove) {
const index = array.indexOf(itemToRemove);
if (index !== -1) {
array = array.slice(0, index)
.concat(array.slice(index + 1));
}
return array;
}
// Driver code
const arr = [ 10, 20, 30, 40, 50 ];
// Remove item 30 from array
console.log("New Array: ", removeItem(arr, 30));
OutputNew Array: [ 10, 20, 40, 50 ]
Similar Reads
How To Remove Specific JSON Object From Array JavaScript? Removing Specific JSON objects from the array is important because it allows for targeted data manipulation and maintenance within JavaScript applications. we will learn to Remove Specific JSON Object From Array JavaScript. These are the following methods: Table of Content Using filter() methodUsing
3 min read
JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po
3 min read
How to remove an item from an array in AngularJS Scope? In the AngularJS framework, we can manipulate the data that is within the scope, for example, we can perform the operations on an array by removing its elements. This helps us for better efficiency in developing the web application.We can remove the array elements in the applications like to-do list
5 min read
How to remove falsy values from an array in JavaScript ? Falsy/Falsey Values: In JavaScript, there are 7 falsy values, which are given below falsezero(0,-0)empty string("", ' ' , ` `)BigIntZero(0n,0x0n)nullundefinedNaNIn JavaScript, the array accepts all types of falsy values. Let's see some approaches on how we can remove falsy values from an array in Ja
6 min read
How to Remove a Property from All Objects in an Array in JavaScript? To remove a property from all objects in an array in JavaScript, you can use the forEach() method to iterate over each object in the array and use the delete operator to remove the specified property.Example:JavaScriptconst arrayOfObjects = [ { name: "Alice", age: 25, city: "New York" }, { name: "Bo
2 min read
How to Splice Duplicate Item from JavaScript Array? Given an array of numbers or strings containing some duplicate values, the task is to remove these duplicate values from the array without creating a new array or storing the duplicate values anywhere else.Examples: Input: arr = [1, 2, 3, 4, 3, 2, 1];Output: [1,2,3,4]Input: [1, 4, 6, 1, 2, 5, 2, 1,
4 min read
How do I Remove an Array Item in TypeScript? In this article, we will learn about the different ways of removing an item from an array in TypeScript. In TypeScript, an array can be defined using union typing if it contains items of different types. We can use the following methods to remove items from a TypeScript array:Table of ContentUsing t
4 min read
JavaScript - Replace an Item from JS Array These are the following ways to replace an item from the JS array: 1. Using Array IndexingThe Array Indexing approach replaces an item in an array by directly accessing its position using the index and assigning a new value. This method is straightforward, modifies the original array, and is ideal f
2 min read
How to Remove Multiple Elements from Array in JavaScript? Here are various methods to remove multiple elements from an array in JavaScript1. Using filter() MethodThe filter() method creates a new array with the elements that pass the condition. This method does not change the original array.JavaScriptlet a = [1, 2, 3, 4, 5]; let remove = [2, 4]; a = a.filt
3 min read
How to Empty an Array in JavaScript? To empty an array in JavaScript, we can use the array literal. We can directly assign an empty array literal to the variable, it will automatically remove all the elements and make the array empty.1. Empty Array using Array LiteralWe can empty the array using the empty array literal syntax. Syntaxar
2 min read